package cn.ksource.core.cache;
|
|
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.ObjectInputStream;
|
import java.io.ObjectOutputStream;
|
|
/**
|
* @note:序列化工具
|
* @version
|
* @author jxl
|
* @date Apr 7, 2016 5:56:51 PM
|
*/
|
public class SerializeUtil {
|
public static byte[] serialize(Object obj) {
|
if (obj == null) {
|
System.out.println("不能序列化NULL");
|
}
|
byte[] rv=null;
|
ByteArrayOutputStream bos = null;
|
ObjectOutputStream os = null;
|
try {
|
bos = new ByteArrayOutputStream();
|
os = new ObjectOutputStream(bos);
|
os.writeObject(obj);
|
rv = bos.toByteArray();
|
} catch (IOException e) {
|
System.out.println("序列化失败");
|
} finally {
|
try {
|
os.close();
|
bos.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return rv;
|
}
|
|
|
public static Object deserialize(byte[] in) {
|
Object rv=null;
|
ByteArrayInputStream bis = null;
|
ObjectInputStream is = null;
|
try {
|
if(in != null) {
|
bis=new ByteArrayInputStream(in);
|
if(null != bis) {
|
is=new ObjectInputStream(bis);
|
rv=is.readObject();
|
}
|
}
|
} catch (Exception e) {
|
System.out.println("反序列化失败");
|
e.printStackTrace();
|
} finally {
|
try {
|
is.close();
|
bis.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
return rv;
|
}
|
|
}
|