/**
* 序列化
* @param object
* @return
*/
private static byte[] serialize(Object 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) {
logger.error("serialize failed for: ", e);
} finally {
if (oos!=null) {
try {
oos.close();
} catch (Exception e) {
}
}
if (baos!=null) {
try {
baos.close();
} catch (Exception e) {
}
}
}
return null;
}
/**
* 反序列化
* @param bytes
* @return
*/
private static Object unserialize(byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
logger.error("unserialize failed for: ", e);
} finally {
if (bais!=null) {
try {
bais.close();
} catch (Exception e) {
}
}
}
return null;
}