JAVA序列化Serializable
2007-03-28 20:29
344 查看
大家都知道Serializable是一个mark interface,告诉JVM这个对象可以被转换成二进制流来传输.
但是Serializable与Externalizable的转换二进制流的过程是不一样的.
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();
System.out.println("got 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 static void main(String[] args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = new FooImpl();
fooimpl.serialize();
}
render_code();
我们运行这段代码看到的debug信息:
writeReplace invoked
writeObject invoked
readObject invoked
readResolve invoked
当进行序列化的时候:
首先JVM会先调用writeReplace方法,在这个阶段,我们可以进行张冠李戴,将需要进行序列化的对象换成我们指定的对象.
跟着JVM将调用writeObject方法,来将对象中的属性一个个进行序列化,我们可以在这个方法中控制住哪些属性需要序列化.
当反序列化的时候:
JVM会调用readObject方法,将我们刚刚在writeObject方法序列化好的属性,反序列化回来.
然后在readResolve方法中,我们也可以指定JVM返回我们特定的对象(不是刚刚序列化回来的对象).
注意到在writeReplace和readResolve,我们可以严格控制singleton的对象,在同一个JVM中完完全全只有唯一的对象,控制不让singleton对象产生副本.
但是Serializable与Externalizable的转换二进制流的过程是不一样的.
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();
System.out.println("got 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 static void main(String[] args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = new FooImpl();
fooimpl.serialize();
}
render_code();
我们运行这段代码看到的debug信息:
writeReplace invoked
writeObject invoked
readObject invoked
readResolve invoked
当进行序列化的时候:
首先JVM会先调用writeReplace方法,在这个阶段,我们可以进行张冠李戴,将需要进行序列化的对象换成我们指定的对象.
跟着JVM将调用writeObject方法,来将对象中的属性一个个进行序列化,我们可以在这个方法中控制住哪些属性需要序列化.
当反序列化的时候:
JVM会调用readObject方法,将我们刚刚在writeObject方法序列化好的属性,反序列化回来.
然后在readResolve方法中,我们也可以指定JVM返回我们特定的对象(不是刚刚序列化回来的对象).
注意到在writeReplace和readResolve,我们可以严格控制singleton的对象,在同一个JVM中完完全全只有唯一的对象,控制不让singleton对象产生副本.
相关文章推荐
- Java 序列化Serializable详解(附详细例子)
- Java 序列化Serializable详解
- java.io.NotSerializableException: 没实现序列化对象而写入文件流的报错
- java序列化--Serializable
- JAVA 序列化Serializable
- java序列化(Serializable)的作用和反序列化
- java Serializable和Externalizable序列化反序列化详解--转
- 序列化:java.io.Serializable
- Java序列化之Serializable
- Java 序列化Serializable详解
- Java 序列化 Serializable
- Android开发时图片Bitmap序列化问题(How to fix a java.io.NotSerializableException: android.graphics.Bitmap)
- Java IO--对象序列化Serializable、ObjectOutputStream、ObjectInputStream、transient
- K:java中序列化的两种方式—Serializable或Externalizable
- Java 序列化 (Serializable)
- 深入理解Java的序列化和反序列化机制——Serializable
- java序列化(Serializable)的作用和反序列化
- java.io 序列化 总结Serializable 和 Externalizable 的区别与联系,以及输出后的乱码问题
- Java序列化Serializable和ObjectOutputStream
- Java序列化(Serializable)与反序列化