ITeye博客

hessian学习基础篇——序列化和反序列化

lionbule · 2009-10-18 13:33 · Java编程 · 企业架构 · OS · Markdown

1、概念介绍

   把Java对象转换为字节序列的过程称为对象的序列化。
   把字节序列恢复为Java对象的过程称为对象的反序列化。

   对象的序列化主要有两种用途:
  1) 数据介质存储
  2) 数据网络传输

2、对象序列化实例

     为了更好的理解hessian的序列化机制,所以把java和hessian的对象序列化实例都一一列出。

      1)对象序列化--java

	public byte[] serialize(Object obj) throws Exception {
		if(obj==null) throw new NullPointerException();
		
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ObjectOutputStream out = new ObjectOutputStream(os);
		out.writeObject(obj);
		return os.toByteArray();
	}
	
	public Object deserialize(byte[] by) throws Exception {
		if(by==null) throw new NullPointerException();
		
		ByteArrayInputStream is = new ByteArrayInputStream(by);
		ObjectInputStream in = new ObjectInputStream(is);
		return in.readObject();
	}

 

      2)对象序列化--hessian (hessian2的序列化方式在附件中)

 

	public 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 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();
	}

 

         从以上代码不难看出,对象序列化的过程为:

         先将对象转为字节码或其它,然后再将其还原为对象。在反序列化时,内存中必须有源对象的所属类。

 

3、对象序列化效率

     hessian2在这方面有了很大的改进,所以优势十分明显。具体细节不再详述,在进阶篇中,我会详述序列化的实现细节。在此仅把实例执行结果公布出来:

java:
77
stxm

hessian:
41
stxm

hessian2:
30
stxm

说明:
1、数字为对象序列化后的字节长度。
2、‘stxm’为对象方法的执行结果。

 

    hessian2的优点,谁用谁知道。待我再做深入研究之后,再把我自己认为的和大家认为的优点总结一下,并加以解释。

评论(6)

  1. #6 楼 · lionbule · 2011-11-23 14:34

    请参考如下性能测试结果: http://code.google.com/p/thrift-protobuf-compare/wiki/Benchmarking

  2. #5 楼 · gaopengxiang417 · 2011-11-20 19:16

    dongbiying writes:

    我测试怎么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不是说序列化的时候时间减少,而是序列化以后在网络上传输的时候能够减少时间(因为流中的字节数减少了一半).

  3. #4 楼 · dongbiying · 2011-11-16 18:06

    我测试怎么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();   
    }
    
    这是我的测试代码 ,请指教呀!
    
  4. #3 楼 · J-catTeam · 2010-11-28 19:09

    haiyupeter writes:

    如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。

    不会的,hessian序列化快的原因是因为它的描述信息比java的少,他用简单的方式描述必要的信息。采取键值对的方式。 所以在序列化一些复杂对象上是有问题的

  5. #2 楼 · lionbule · 2010-08-29 12:22

    haiyupeter writes:

    如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。

    我坚信“能量守恒定律”。不管哪种方法,只要在实现原理上没有进行彻底的革新,就不会有太大的改观。

    换句话说,不是用空间换时间,就是用时间换空间。

    所以我们更多关注的应该是实现原理,多看源码就晓得其实每个都差不多。

    如果有一天能是“汽车换马车”,就会有很大进步了。期望……

  6. #1 楼 · haiyupeter · 2010-08-16 13:26

    如果能把序列化后的字节减少,那么问一下会带来多少CPU的损耗。

评论功能维护中,当前仅展示历史评论。