Java技巧:序列化的两种方式

大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输.
Serializable 在我们实现这个接口的时候,我们可以使用4个私有方法来控制序列化的过程: 
我们来看一个例子:
public class FooImpl implements java.io.Serializable{ private String message; public String getFoo() { return message; } public void setMessage(String message) { this.message = message; } private void writeObject(java.io.ObjectOutputStream out) throws IOException { System.out.println("writeObject invoked"); out.writeObject(this.message == null ? "hohohahaha" : this.message); } private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { System.out.println("readObject invoked"); this.message = (String) in.readObject(); } private Object writeReplace() throws ObjectStreamException { System.out.println("writeReplace invoked"); return this; } private Object readResolve() throws ObjectStreamException { System.out.println("readResolve invoked"); return this; } public Object serialize() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } public static void main(String[] args) throws IOException, ClassNotFoundException { FooImpl fooimpl = new FooImpl(); fooimpl.serialize(); } }
我们运行这段代码看到的debug信息:
writeReplace invoked
writeObject invoked
readObject invoked
readResolve invoked

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

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


Externalizable 是一个有实际方法需要实现的interface,包括writeExternal和readExternal,是Serializable接口的子接口:
public class FooImpl implements java.io.Externalizable { private String message; public String getFoo() { return message; } public void setMessage(String message) { this.message = message; } private Object writeReplace() throws ObjectStreamException { System.out.println("writeReplace invoked"); return this; } private Object readResolve() throws ObjectStreamException { System.out.println("readResolve invoked"); return this; } public Object serialize() throws IOException, ClassNotFoundException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(this); ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bais); return ois.readObject(); } public void readExternal(ObjectInput arg0) throws IOException, ClassNotFoundException { System.out.println("readExternal invoked"); Object obj = arg0.readObject(); } public void writeExternal(ObjectOutput arg0) throws IOException { System.out.println("writeExternal invoked"); arg0.writeObject("Hello world"); } public static void main(String[] args) throws IOException, ClassNotFoundException { FooImpl fooimpl = new FooImpl(); fooimpl.serialize(); } }
我们运行这段代码看到的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序列化的内容.

你可能感兴趣的:(java,jvm,object,String,Class,interface)