package com.qxueyou.scc.base.util;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
|
import org.apache.commons.io.IOUtils;
|
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.Logger;
|
|
public class ReisUtils {
|
private static Logger log = LogManager.getLogger(ReisUtils.class);
|
|
/**
|
* 序列化对象
|
*/
|
public static byte[] serialize(Object obj){
|
ObjectOutputStream objectOs = null;
|
ByteArrayOutputStream byteArrOs = null;
|
byte[] bytes = null;
|
try {
|
byteArrOs = new ByteArrayOutputStream();
|
objectOs = new ObjectOutputStream(byteArrOs);
|
objectOs.writeObject(obj);
|
bytes = byteArrOs.toByteArray();
|
} catch (Exception e) {
|
log.error("序列化对象失败!");
|
}finally {
|
IOUtils.closeQuietly(objectOs);
|
IOUtils.closeQuietly(byteArrOs);
|
}
|
return bytes;
|
}
|
|
/**
|
* 反序列化对象
|
*/
|
public static Object unserialize(byte[] bytes){
|
Object returnObject = null;
|
ByteArrayInputStream byteArrIns = null;
|
ObjectInputStream objectIns = null;
|
try {
|
if (bytes != null && bytes.length!=0){
|
byteArrIns = new ByteArrayInputStream(bytes);
|
objectIns = new ObjectInputStream(byteArrIns);
|
returnObject = objectIns.readObject();
|
}
|
}catch (Exception e) {
|
log.error("反序列化对象失败!",e);
|
}finally {
|
IOUtils.closeQuietly(objectIns);
|
IOUtils.closeQuietly(byteArrIns);
|
}
|
return returnObject;
|
}
|
|
|
// public static void main(String[] args) throws IOException{
|
// Object s = null;
|
// byte[] b = serialize(s);
|
// Object obj= unserialize(b);
|
// System.out.println(s);
|
// }
|
|
|
}
|