hessian学习基础篇——序列化和反序列化
1、概念介绍
?? 把Java对象转换为字节序列的过程称为对象的序列化。
?? 把字节序列恢复为Java对象的过程称为对象的反序列化。
?? 对象的序列化主要有两种用途:
1) 数据介质存储
2) 数据网络传输
2、对象序列化实例
???? 为了更好的理解hessian的序列化机制,所以把java和hessian的对象序列化实例都一一列出。
????? 1)对象序列化--java
java:77stxmhessian:41stxmhessian2:30stxm说明:1、数字为对象序列化后的字节长度。2、‘stxm’为对象方法的执行结果。
?
??? hessian2的优点,谁用谁知道。待我再做深入研究之后,再把我自己认为的和大家认为的优点总结一下,并加以解释。
?
1 楼 haiyupeter 2010-08-16 如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。 2 楼 lionbule 2010-08-29 haiyupeter 写道如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。我坚信“能量守恒定律”。不管哪种方法,只要在实现原理上没有进行彻底的革新,就不会有太大的改观。
换句话说,不是用空间换时间,就是用时间换空间。
所以我们更多关注的应该是实现原理,多看源码就晓得其实每个都差不多。
如果有一天能是“汽车换马车”,就会有很大进步了。期望…… 3 楼 J-catTeam 2010-11-28 haiyupeter 写道如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。
不会的,hessian序列化快的原因是因为它的描述信息比java的少,他用简单的方式描述必要的信息。采取键值对的方式。 所以在序列化一些复杂对象上是有问题的 4 楼 dongbiying 2011-11-16 我测试怎么java的序列化时间比hessian的少呢,
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀! 5 楼 gaopengxiang417 2011-11-20 dongbiying 写道我测试怎么java的序列化时间比hessian的少呢,
你测试的是对象吗 ?
public static void main(String[] args) throws IOException {
//hessian
Long startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = javaSerialize(new User("dby"+i));
}
System.out.println("-----java--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize2(new User("dby"+i));
}
System.out.println("-----hessian2--"+(System.currentTimeMillis()-startLong));
startLong = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
byte[] bytes = serialize(new User("dby"+i));
}
System.out.println("-----hessian--"+(System.currentTimeMillis()-startLong));
}
public static byte[] serialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
HessianInput hi = new HessianInput(is);
return hi.readObject();
}
public static byte[] serialize2(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
Hessian2Output ho = new Hessian2Output(os);
ho.writeObject(obj);
return os.toByteArray();
}
public static Object deserialize2(byte[] by) throws IOException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
Hessian2Input hi = new Hessian2Input(is);
return hi.readObject();
}
public static byte[] javaSerialize(Object obj) throws IOException{
if(obj==null) throw new NullPointerException();
ByteArrayOutputStream os = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(os);
oos.writeObject(obj);
return os.toByteArray();
}
public static Object javaDeserialize(byte[] by) throws IOException, ClassNotFoundException{
if(by==null) throw new NullPointerException();
ByteArrayInputStream is = new ByteArrayInputStream(by);
ObjectInputStream in = new ObjectInputStream(is);
return in.readObject();
}
这是我的测试代码 ,请指教呀!
hession不是说序列化的时候时间减少,而是序列化以后在网络上传输的时候能够减少时间(因为流中的字节数减少了一半). 6 楼 lionbule 2011-11-23 请参考如下性能测试结果:
http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking