您的位置:首页 > 其它

对象序列化

2014-03-14 11:20 155 查看
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();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: