JAVA序列化基础知识Serializable与Externalizable的区别

来源:互联网 发布:java执行ping命令 编辑:程序博客网 时间:2024/04/29 12:36

转载:http://dovecat.iteye.com/blog/66044

大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输. 

但是Serializable与Externalizable的转换二进制流的过程是不一样的. 
Serializable 在我们实现这个接口的时候,我们可以使用4个私有方法来控制序列化的过程: 

  我们来看一个例子: 

[java] view plaincopy
  1. package test1;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInputStream;  
  7. import java.io.ObjectOutputStream;  
  8. import java.io.ObjectStreamException;  
  9.   
  10. public class FooImpl0 implements java.io.Serializable {  
  11.     private String message;  
  12.   
  13.     public String getFoo() {  
  14.         return message;  
  15.     }  
  16.   
  17.     public void setMessage(String message) {  
  18.         this.message = message;  
  19.     }  
  20.   
  21.     private void writeObject(java.io.ObjectOutputStream out) throws IOException {  
  22.         System.out.println("writeObject invoked");  
  23.         out.writeObject(this.message == null ? "hohohahaha" : this.message);  
  24.     }  
  25.   
  26.     private void readObject(java.io.ObjectInputStream in) throws IOException,  
  27.             ClassNotFoundException {  
  28.         System.out.println("readObject invoked");  
  29.         this.message = (String) in.readObject();  
  30.         System.out.println("got message:" + message);  
  31.     }  
  32.   
  33.     private Object writeReplace() throws ObjectStreamException {  
  34.         System.out.println("writeReplace invoked");  
  35.         return this;  
  36.     }  
  37.   
  38.     private Object readResolve() throws ObjectStreamException {  
  39.         System.out.println("readResolve invoked");  
  40.         return this;  
  41.     }  
  42.   
  43.     public Object serialize() throws IOException, ClassNotFoundException {  
  44.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  45.         ObjectOutputStream oos = new ObjectOutputStream(baos);  
  46.         oos.writeObject(this);  
  47.         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  
  48.         ObjectInputStream ois = new ObjectInputStream(bais);  
  49.         return ois.readObject();  
  50.     }  
  51.   
  52.     public static void main(String[] args) throws IOException,  
  53.             ClassNotFoundException {  
  54.         FooImpl0 fooimpl = new FooImpl0();  
  55.         fooimpl.serialize();  
  56.     }  
  57. }  


我们运行这段代码看到的debug信息: 

writeReplace invoked 
writeObject invoked 
readObject invoked 
readResolve invoked 


当进行序列化的时候: 
首先JVM会先调用writeReplace方法,在这个阶段,我们可以进行张冠李戴,将需要进行序列化的对象换成我们指定的对象. 
跟着JVM将调用writeObject方法,来将对象中的属性一个个进行序列化,我们可以在这个方法中控制住哪些属性需要序列化. 

当反序列化的时候: 
JVM会调用readObject方法,将我们刚刚在writeObject方法序列化好的属性,反序列化回来. 
然后在readResolve方法中,我们也可以指定JVM返回我们特定的对象(不是刚刚序列化回来的对象). 

注意到在writeReplace和readResolve,我们可以严格控制singleton的对象,在同一个JVM中完完全全只有唯一的对象,控制不让singleton对象产生副本. 


Externalizable 是一个有实际方法需要实现的interface,包括writeExternal和readExternal: 

[java] view plaincopy
  1. package test1;  
  2.   
  3. import java.io.ByteArrayInputStream;  
  4. import java.io.ByteArrayOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.ObjectInput;  
  7. import java.io.ObjectInputStream;  
  8. import java.io.ObjectOutput;  
  9. import java.io.ObjectOutputStream;  
  10. import java.io.ObjectStreamException;  
  11.   
  12. public class FooImpl implements java.io.Externalizable {  
  13.     private String message;  
  14.   
  15.     public String getFoo() {  
  16.         return message;  
  17.     }  
  18.   
  19.     public void setMessage(String message) {  
  20.         this.message = message;  
  21.     }  
  22.   
  23.     private Object writeReplace() throws ObjectStreamException {  
  24.         System.out.println("writeReplace invoked");  
  25.         return this;  
  26.     }  
  27.   
  28.     private Object readResolve() throws ObjectStreamException {  
  29.         System.out.println("readResolve invoked");  
  30.         return this;  
  31.     }  
  32.   
  33.     public Object serialize() throws IOException, ClassNotFoundException {  
  34.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  35.         ObjectOutputStream oos = new ObjectOutputStream(baos);  
  36.         oos.writeObject(this);  
  37.         ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());  
  38.         ObjectInputStream ois = new ObjectInputStream(bais);  
  39.         return ois.readObject();  
  40.     }  
  41.   
  42.     public void readExternal(ObjectInput arg0) throws IOException,  
  43.             ClassNotFoundException {  
  44.         System.out.println("readExternal invoked");  
  45.         Object obj = arg0.readObject();  
  46.         System.out.println(obj.toString());  
  47.     }  
  48.   
  49.     public void writeExternal(ObjectOutput arg0) throws IOException {  
  50.         System.out.println("writeExternal invoked");  
  51.         arg0.writeObject("Hello world");  
  52.     }  
  53.   
  54.     public static void main(String[] args) throws IOException,  
  55.             ClassNotFoundException {  
  56.         FooImpl fooimpl = new FooImpl();  
  57.         fooimpl.serialize();  
  58.     }  
  59. }  


我们运行这段代码看到的debug信息: 

writeReplace invoked 
writeExternal invoked 
readExternal invoked 
readResolve invoked 
在此writeExternal 和readExternal 的作用与writeObject和readObject 一样. 

最后,当我们同时实现了两个interface的时候,JVM只运行Externalizable 接口里面的writeExternal 和readExternal 方法对序列化内容进行处理. 
需要注意的是:Serializable是一个真正的mark interface, 
writeObject,readObject, writeReplace,readResolve是直接与JVM通信,告诉JVM序列化的内容.
0 0
原创粉丝点击