java序列化:JAVA序列化的两种方式

大家都知道Serializable是个mark erface,告诉JVM这个对象可以被转换成 2进制流来传输.
Serializable 在我们实现这个接口时候,我们可以使用4个私有思路方法来控制序列化过程:
我们来看个例子:

public FooImpl implements java.io.Serializable{
private String message;

public String getFoo {
message;
}

public void Message(String message) {
this.message = message;
}

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
.out.prln("writeObject invoked");
out.writeObject(this.message null ? "hohohahaha" : this.message);
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
.out.prln("readObject invoked");
this.message = (String) in.readObject;
}

private Object writeReplace throws ObjectStreamException {
.out.prln("writeReplace invoked");
this;
}

private Object readResolve throws ObjectStreamException {
.out.prln("readResolve invoked");
this;
}

public Object serialize throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = ByteArrayOutputStream;
ObjectOutputStream oos = ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = ByteArrayInputStream(baos.toByteArray);
ObjectInputStream ois = ObjectInputStream(bais);
ois.readObject;
}
public void (String args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = FooImpl;
fooimpl.serialize;
}
}
public FooImpl implements java.io.Serializable{
private String message;

public String getFoo {
message;
}

public void Message(String message) {
this.message = message;
}

private void writeObject(java.io.ObjectOutputStream out) throws IOException {
.out.prln("writeObject invoked");
out.writeObject(this.message null ? "hohohahaha" : this.message);
}

private void readObject(java.io.ObjectInputStream in) throws IOException,
ClassNotFoundException {
.out.prln("readObject invoked");
this.message = (String) in.readObject;
}

private Object writeReplace throws ObjectStreamException {
.out.prln("writeReplace invoked");
this;
}

private Object readResolve throws ObjectStreamException {
.out.prln("readResolve invoked");
this;
}

public Object serialize throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = ByteArrayOutputStream;
ObjectOutputStream oos = ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = ByteArrayInputStream(baos.toByteArray);
ObjectInputStream ois = ObjectInputStream(bais);
ois.readObject;
}
public void (String args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = FooImpl;
fooimpl.serialize;
}
}
我们运行这段代码看到debug信息:
writeReplace invoked
writeObject invoked
readObject invoked
readResolve invoked

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

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


Externalizable 是个有实际思路方法需要实现erface,包括writeExternal和readExternal,是Serializable接口子接口:
view plaincopy to clipboardpr?
public FooImpl implements java.io.Externalizable {
private String message;

public String getFoo {
message;
}

public void Message(String message) {
this.message = message;
}

private Object writeReplace throws ObjectStreamException {
.out.prln("writeReplace invoked");
this;
}

private Object readResolve throws ObjectStreamException {
.out.prln("readResolve invoked");
this;
}

public Object serialize throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = ByteArrayOutputStream;
ObjectOutputStream oos = ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = ByteArrayInputStream(baos.toByteArray);
ObjectInputStream ois = ObjectInputStream(bais);
ois.readObject;
}

public void readExternal(ObjectInput arg0) throws IOException,
ClassNotFoundException {
.out.prln("readExternal invoked");
Object obj = arg0.readObject;
}

public void writeExternal(ObjectOutput arg0) throws IOException {
.out.prln("writeExternal invoked");
arg0.writeObject("Hello world");
}
public void (String args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = FooImpl;
fooimpl.serialize;
}
}
public FooImpl implements java.io.Externalizable {
private String message;

public String getFoo {
message;
}

public void Message(String message) {
this.message = message;
}

private Object writeReplace throws ObjectStreamException {
.out.prln("writeReplace invoked");
this;
}

private Object readResolve throws ObjectStreamException {
.out.prln("readResolve invoked");
this;
}

public Object serialize throws IOException, ClassNotFoundException {
ByteArrayOutputStream baos = ByteArrayOutputStream;
ObjectOutputStream oos = ObjectOutputStream(baos);
oos.writeObject(this);
ByteArrayInputStream bais = ByteArrayInputStream(baos.toByteArray);
ObjectInputStream ois = ObjectInputStream(bais);
ois.readObject;
}

public void readExternal(ObjectInput arg0) throws IOException,
ClassNotFoundException {
.out.prln("readExternal invoked");
Object obj = arg0.readObject;
}

public void writeExternal(ObjectOutput arg0) throws IOException {
.out.prln("writeExternal invoked");
arg0.writeObject("Hello world");
}
public void (String args) throws IOException,
ClassNotFoundException {
FooImpl fooimpl = FooImpl;
fooimpl.serialize;
}
}
我们运行这段代码看到debug信息:
writeReplace invoked
writeExternal invoked
readExternal invoked
readResolve invoked
在此writeExternal 和readExternal 作用和writeObject和readObject 样.

最后,当我们同时实现了两个erface时候,JVM只运行Externalizable 接口里面writeExternal 和readExternal 思路方法对序列化内容进行处理.
需要注意是:Serializable是个真正mark erface,
writeObject,readObject, writeReplace,readResolve是直接和JVM通信,告诉JVM序列化内容.
Tags:  什么是java序列化 java反序列化 java对象序列化 java序列化

延伸阅读

最新评论

发表评论