package com.gx.obe.server.common.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public final class SerializeUtils {
	
	private static final org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(SerializeUtils.class);
	
	/**
	 * @Description: 序列化对象
	 * @author guoyr
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static byte[] serializeObject(Object object) {
		if (null == object) {
			return null;
		}
		byte[] result = null;
		ByteArrayOutputStream saos = new ByteArrayOutputStream();
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(saos);
			oos.writeObject(object);
			oos.flush();
			result = saos.toByteArray();
		} catch (IOException e) {
			LOG.error("序列化对象时出错!", e);
		} finally {
			if (null != oos) {
				try {
					oos.close();
				} catch (IOException e) {}
			}
			if (null != saos) {
				try {
					saos.close();
				} catch (IOException e) {}
			}
		}
		return result;
	}
	
	/**
	 * @Description: 序列化对象
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static void serializeObjectFile(Object object, String filePath) {
		FileOutputStream fs = null;
		ObjectOutputStream os = null;
		try {
			File file = new File(filePath);
			if (null != file) {
				if (file.exists()) {
					file.delete();
				} else if (!file.getParentFile().exists()) {
					file.getParentFile().mkdirs();
				}
				fs = new FileOutputStream(filePath);
				os = new ObjectOutputStream(fs);
				os.writeObject(object);
				os.flush();
			}
		} catch (Exception e) {
			LOG.error("序列化对象时出错!", e);
		} finally {
			if (null != os) {
				try {
					os.close();
				} catch (IOException e) {}
			}
			if (null != fs) {
				try {
					fs.close();
				} catch (IOException e) {}
			}
		}
	}
	
	/**
	 * @Description: 反序列化对象
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @param object
	 * @return
	 * @throws IOException
	 */
	public static Object deserializeObject(File file) {
		Object object = null;
		if (file == null || !file.exists() || !file.isFile()) {
			LOG.error("不是有效的文件");
			return null;
		}
		FileInputStream in = null;
		ObjectInputStream ois = null;
		try {
			in = new FileInputStream(file);
			try {
				ois = new ObjectInputStream(in);
				try {
					object = ois.readObject();
				} catch (ClassNotFoundException e) {
					LOG.error("反序列化对象时出错!", e);
				}
				ois.close();
			} catch (IOException e) {
				LOG.error("反序列化对象时出错!", e);
			}
		} catch (FileNotFoundException e) {
			LOG.error("反序列化对象时出错!", e);
		} finally {
			if (null != ois) {
				try {
					ois.close();
				} catch (IOException e) {}
			}
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {}
			}
		}
		return object;
	}
	
	/**
	 * @Description: 反序列化对象
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @author guoyr
	 * @param buf
	 * @return
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	@SuppressWarnings("unchecked")
	public static <T> T deserializeObject(byte[] buf) {
		Object object = null;
		if (null == buf) {
			return null;
		}
		ByteArrayInputStream sais = new ByteArrayInputStream(buf);
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(sais);
			try {
				object = ois.readObject();
			} catch (ClassNotFoundException e) {
				LOG.error("反序列化对象时出错!", e);
			}
		} catch (IOException e) {
			LOG.error("反序列化对象时出错!", e);
		} finally {
			if (null != ois) {
				try {
					ois.close();
				} catch (IOException e) {}
			}
			if (null != sais) {
				try {
					sais.close();
				} catch (IOException e) {}
			}
		}
		return (T) object;
	}
	
}