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

  1. 云栖社区>
  2. 博客>
  3. 正文

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

无声胜有声 2017-04-03 12:57:00 浏览428
展开阅读全文

如:

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

网友评论

登录后评论
0/500
评论
无声胜有声
+ 关注