码迷,mamicode.com
首页 > 其他好文 > 详细

序列化与反序列化的单例模式实现和readResolve()【转】

时间:2017-04-03 13:18:00      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:htm   tac   shared   getc   puts   调用   inpu   run   adr   

如:

public class SingleTest implements Serializable{
private static final long serialVersionUID = -8600246627673134435L;
private static class SingleTestHandler{
private static SingleTest singleTest=new SingleTest();
}
private SingleTest() {
}
public static SingleTest getInstance(){
return SingleTestHandler.singleTest;
}
/* protected Object readResolve(){
System.out.println("调用了readResolve方法!");
return SingleTestHandler.singleTest;
}*/
}
class MyThead extends Thread{
public void run(){
System.out.println(SingleTest.getInstance().hashCode());
}
}
class Test1{
public static void main(String[] args) {
try {
SingleTest singleTest=SingleTest.getInstance();
FileOutputStream fileOutputStream=new FileOutputStream(new File("myObjectFilee.txt"));
ObjectOutputStream objectOutputStream=new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(singleTest);
objectOutputStream.close();
fileOutputStream.close();
System.out.println(singleTest.hashCode());
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fileInputStream=new FileInputStream(new File("myObjectFilee.txt"));
ObjectInputStream objectInputStream=new ObjectInputStream(fileInputStream);
SingleTest singleTest=(SingleTest) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
System.out.println(singleTest.hashCode());
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
加上注释运行结果证明返回的不是同一个实例:
692404036
931919113
-----------------------------------------------------------------------------------------------------------------------------
去掉注释运行结果为:
692404036
调用了readResolve方法!
692404036
证明为同一个实例
-----------------------------------------------------------------------------------------------------------------------------
那么这个readResolve()方法是从哪来的,为什么加上之后就能返回同一实例了呢?
找到ObjectInputStream类的
/**
* Reads and returns "ordinary" (i.e., not a String, Class,
* ObjectStreamClass, array, or enum constant) object, or null if object‘s
* class is unresolvable (in which case a ClassNotFoundException will be
* associated with object‘s handle). Sets passHandle to object‘s assigned
* handle.
*/
private Object readOrdinaryObject(boolean unshared)
throws IOException
{
if (bin.readByte() != TC_OBJECT) {
throw new InternalError();
}

ObjectStreamClass desc = readClassDesc(false);
desc.checkDeserialize();

Class<?> cl = desc.forClass();
if (cl == String.class || cl == Class.class
|| cl == ObjectStreamClass.class) {
throw new InvalidClassException("invalid class descriptor");
}

Object obj;
try {
obj = desc.isInstantiable() ? desc.newInstance() : null;
} catch (Exception ex) {
throw (IOException) new InvalidClassException(
desc.forClass().getName(),
"unable to create instance").initCause(ex);
}

passHandle = handles.assign(unshared ? unsharedMarker : obj);
ClassNotFoundException resolveEx = desc.getResolveException();
if (resolveEx != null) {
handles.markException(passHandle, resolveEx);
}

if (desc.isExternalizable()) {
readExternalData((Externalizable) obj, desc);
} else {
readSerialData(obj, desc);
}

handles.finish(passHandle);

if (obj != null &&
handles.lookupException(passHandle) == null &&
desc.hasReadResolveMethod())
{
Object rep = desc.invokeReadResolve(obj);
if (unshared && rep.getClass().isArray()) {
rep = cloneArray(rep);
}
if (rep != obj) {
handles.setObject(passHandle, obj = rep);
}
}

return obj;
}

转自:http://www.cnblogs.com/345214483-qq/p/6472158.html

序列化与反序列化的单例模式实现和readResolve()【转】

标签:htm   tac   shared   getc   puts   调用   inpu   run   adr   

原文地址:http://www.cnblogs.com/rwxwsblog/p/6661719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!