package com.vincent.rsf.common.utils;
|
|
import java.io.*;
|
|
public class Serialize {
|
|
// 序列化
|
public static byte[] serialize(Object object) {
|
ObjectOutputStream oos = null;
|
ByteArrayOutputStream baos = null;
|
try {
|
baos = new ByteArrayOutputStream();
|
oos = new ObjectOutputStream(baos);
|
oos.writeObject(object);
|
return baos.toByteArray();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
// 反序列化
|
public static Object unSerialize(byte[] bytes) {
|
ByteArrayInputStream bais = null;
|
try {
|
|
bais = new ByteArrayInputStream(bytes);
|
ObjectInputStream ois = new ObjectInputStream(bais){
|
@Override
|
protected Class<?> resolveClass(ObjectStreamClass desc)
|
throws IOException, ClassNotFoundException {
|
return Class.forName( desc.getName(), true, Thread.currentThread().getContextClassLoader());
|
}
|
};
|
return ois.readObject();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
|
}
|