java对象序列化-SerializableUtils源码
2017-03-09 10:33
369 查看
来自:http://jinnianshilongnian.iteye.com/blog/2028675
SerializableUtils
package com.github.zhangkaitao.shiro.chapter10;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.Session;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* <p>User: Zhang Kaitao
* <p>Date: 14-2-8
* <p>Version: 1.0
*/
public class SerializableUtils {
public static String serialize(Session session) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(session);
return Base64.encodeToString(bos.toByteArray());
} catch (Exception e) {
throw new RuntimeException("serialize session error", e);
}
}
public static Session deserialize(String sessionStr) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
ObjectInputStream ois = new ObjectInputStream(bis);
return (Session)ois.readObject();
} catch (Exception e) {
throw new RuntimeException("deserialize session error", e);
}
}
}
SerializableUtils
package com.github.zhangkaitao.shiro.chapter10;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.session.Session;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* <p>User: Zhang Kaitao
* <p>Date: 14-2-8
* <p>Version: 1.0
*/
public class SerializableUtils {
public static String serialize(Session session) {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(session);
return Base64.encodeToString(bos.toByteArray());
} catch (Exception e) {
throw new RuntimeException("serialize session error", e);
}
}
public static Session deserialize(String sessionStr) {
try {
ByteArrayInputStream bis = new ByteArrayInputStream(Base64.decode(sessionStr));
ObjectInputStream ois = new ObjectInputStream(bis);
return (Session)ois.readObject();
} catch (Exception e) {
throw new RuntimeException("deserialize session error", e);
}
}
}
相关文章推荐
- Java对象序列化
- java对象序列化学习笔记
- 通过socket连接传递java对象(对象序列化
- java对象序列化(二)
- Java对象序列化(一)
- java对象序列化学习笔记(z)
- java对象序列化学习笔记
- Java学习之对象序列化(二)
- RoyalAjax(三),Java对象的序列化
- 小谈java中的对象序列化
- JAVA的对象序列化
- Java学习之对象序列化(二)
- Java学习之对象序列化
- 通过socket连接传递java对象(对象序列化)
- Java中如何将XML对象反序列化
- [Java]java对象序列化学习笔记
- JAVA对象序列化保存为XML文件的工具类
- 关于 Ajax 的 Java 对象序列化 及获得数据后如何展现出来
- 将java的对象序列化成文件,并从文件中反序列化。
- java对象序列化学习笔记(zz)