Java——序列化 反序列化
2024-09-03 11:35:29
记录一下:
先粘两个比较繁琐的方法:
put:
public void putSerializableObject(String key, Object value, int expireTime) {
key = preProcessKey(key);
ByteArrayOutputStream byteArrayOutputStream = null;
ObjectOutputStream objectOutputStream = null;
try {
byteArrayOutputStream = new ByteArrayOutputStream();
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(value);
byte[] bytes = byteArrayOutputStream.toByteArray();
cluster.add(key, expireTime, bytes);
} catch (Exception e) {
logger.warn("Memcache put() failed! key=" + key, e);
} finally {
if (byteArrayOutputStream != null) {
try {
byteArrayOutputStream.close();
} catch (IOException e) {
logger.warn("ByteArrayOutputStream close() failed! key=" + key + e);
}
}
if (objectOutputStream != null) {
try {
objectOutputStream.close();
} catch (IOException e) {
logger.warn("ObjectOutputStream close() failed! key=" + key + e);
}
}
}
}
get:
public <T> T getSerializableObject(String key, Class<T> clazz) {
key = preProcessKey(key);
T result = null;
ByteArrayInputStream byteArrayInputStream = null;
ObjectInputStream objectInputStream = null;
try {
byte[] resultByte = (byte[]) cluster.get(key);
if (null != resultByte) {
byteArrayInputStream = new ByteArrayInputStream(resultByte);
objectInputStream = new ObjectInputStream(byteArrayInputStream);
result = (T) objectInputStream.readObject();
}
} catch (Exception e) {
logger.warn("Memcache get() failed! key=" + key, e);
return null;
} finally {
if (objectInputStream != null) {
try {
objectInputStream.close();
} catch (IOException e) {
logger.warn("ObjectInputStream close() failed ! key=" + key + e);
}
}
if (byteArrayInputStream != null) {
try {
byteArrayInputStream.close();
} catch (IOException e) {
logger.warn("ByteArrayInputStream close() failed ! key=" + key + e);
}
}
}
return result;
}
重点!
上面两个方法,有冗余的代码,可以进一步简化:
序列化:
public static byte[] serialize(Object object) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
oos.writeObject(object);
return baos.toByteArray();
}
}
反序列化:
public static Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException {
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
try (ObjectInputStream ois = new ObjectInputStream(bais)) {
return ois.readObject();
}
}
由于 InputStream继承了Closeable,当在try-cache中使用流的时候,会在执行结束try-cache后自动调用close方法,无论是否抛出异常,代码简洁多了,很棒棒哦 (*^▽^*)~
最新文章
- fir.im Weekly - APP 性能监测优化 二三事
- Centos 上 Tengine安装
- 怎样解决Myeclipse中运行jsp乱码问题,亲测有效(虽然是个小问题但是为了大家不被网络上的一些乱七八糟的回答坑)不是改什么windows-propories-...............
- KEIL MDK 5.12帮你快速建工程模板的技巧
- UVALive 4452 The Ministers&#39; Major Mess(2-sat)
- Xml通用操作类
- Oracle查询经典
- 生成N个不重复的随机数(转)
- 一个正整数N,拆成任意个正整数之和,怎样使这些数的乘积最大
- 同一DataTable下创建多个结构数据相同的DataView的小问题
- android 定时器总结
- Windows下编译mxnet
- Spring中实现文件上传
- Uva 10550 Combination Lock
- Asp.NETCore轻松学系列阅读指引目录
- Spark应用【根据新df更新旧df】
- java 得到目录路径的方法
- JAVA自学笔记14
- 学习 MeteoInfo二次开发教程(一)
- POJ 1741 Tree(点分治点对<;=k)