对象序列化

public static <T> T getObjectFromBytes(byte[] buffer, Class<T> clazz) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = null;
ObjectInputStream ois = null;
try {
bais = new ByteArrayInputStream(buffer);
ois = new ObjectInputStream(bais);
Object obj = ois.readObject();
return (T) obj;
} finally {
if (bais != null)
bais.close();
}
}


public static byte[] getBytesFromObject(Serializable serializable) throws IOException {
ByteArrayOutputStream baos = null;
ObjectOutputStream oos = null;
try {
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(serializable);
oos.flush();
return baos.toByteArray();
} finally {
baos.close();
}
}

你可能感兴趣的:(对象序列化)