对象序列化
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();
}
}
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();
}
}
相关文章推荐
- App Crashed 记录
- 第二周_C++
- stanford-postagger中文词性标注
- Rando
- UVA 12657/COJ 1329 HN第九届省赛 链表模拟
- Android编写访问http源代码的方法
- 10.5. Accessing VMs
- free函数与内存管理
- 深入理解OutOfMemoryError
- eclipse查找一个字符串
- 关于android的ScrollView套listView展示不全的问题
- 文字编码格式,我总也搞不懂
- 堆和栈
- 2014-03-14
- 为什么判断UITextField判断为空不能用isEqualToString:@""
- 启动ssh服务
- 要是我问的问题你解释的不清楚,我就喂你袋盐!
- Hadoop连载系列之四:数据收集分析系统Chukwa 推荐
- div style="border
- unity自带寻路Navmesh入门教程