package com.ycl.utils;
|
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
|
/**
|
*
|
* 序列化工具
|
* @author liyanqi
|
*/
|
public class SerializeUtil {
|
|
private static Logger logger = LoggerFactory.getLogger(SerializeUtil.class);
|
|
/**
|
*
|
* @Description 序列化
|
* @param object
|
* @return
|
* @throws Exception
|
*/
|
public static byte[] serialize(Object object) throws Exception {
|
if (object == null)
|
return null;
|
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(e.toString());
|
throw e;
|
}
|
}
|
|
/**
|
*
|
* @Description 反序列化
|
* @param bytes
|
* @return
|
* @throws Exception
|
*/
|
public static Object unSerialize(byte[] bytes) throws Exception {
|
if (bytes == null)
|
return null;
|
ByteArrayInputStream bais = null;
|
try {
|
// 反序列化
|
bais = new ByteArrayInputStream(bytes);
|
ObjectInputStream ois = new ObjectInputStream(bais);
|
return ois.readObject();
|
} catch (Exception e) {
|
logger.error(e.toString());
|
throw e;
|
}
|
}
|
}
|