package com.mandi.common;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
|
import org.apache.log4j.Logger;
|
|
|
/**
|
* @author mengly
|
* @version 创建时间:2016年2月27日 下午5:08:30
|
* 类说明
|
*/
|
|
public class SerializeMethod {
|
private static Logger log=Logger.getLogger(SerializeMethod.class);
|
/**
|
* 序列化
|
*
|
* @param object
|
* @return
|
*/
|
public static <T> byte[] serialize(T object) {
|
ObjectOutputStream oos = null;
|
ByteArrayOutputStream baos = null;
|
try {
|
baos = new ByteArrayOutputStream();
|
oos = new ObjectOutputStream(baos);
|
oos.writeObject(object);
|
byte[] bytes = baos.toByteArray();
|
return bytes;
|
} catch (Exception e) {
|
log.info("序列化错误"+e.getMessage());
|
}
|
return null;
|
}
|
|
/**
|
* 反序列化
|
*
|
* @param bytes
|
* @return
|
*/
|
public static <T> T deserialize(byte[] bytes,Class<T> clazz) {
|
ByteArrayInputStream bais = null;
|
try {
|
bais = new ByteArrayInputStream(bytes);
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
Object obj=ois.readObject();
|
@SuppressWarnings("unchecked")
|
T t=(T)obj;
|
return t;
|
} catch (Exception e) {
|
log.info("反序列化错误"+e.getMessage());
|
}
|
return null;
|
}
|
}
|