/** * Serializability of a class is enabled by the class implementing the * java.io.Serializable interface. Classes that do not implement this * interface will not have any of their state serialized or * deserialized. All subtypes of a serializable class are themselves * serializable. The serialization interface has no methods or fields * and serves only to identify the semantics of being serializable. * / public interface Serializable {}
/** * Only the identity of the class of an Externalizable instance is * written in the serialization stream and it is the responsibility * of the class to save and restore the contents of its instances. */ publicinterfaceExternalizableextendsjava.io.Serializable { /** * The object implements the writeExternal method to save its contents * by calling the methods of DataOutput for its primitive values or * calling the writeObject method of ObjectOutput for objects, strings, * and arrays. */ voidwriteExternal(ObjectOutput out)throws IOException;
/** * The object implements the readExternal method to restore its * contents by calling the methods of DataInput for primitive * types and readObject for objects, strings and arrays. The * readExternal method must read the values in the same sequence * and with the same types as were written by writeExternal. */ voidreadExternal(ObjectInput in)throws IOException, ClassNotFoundException; }
/** * Invokes the writeObject method of the represented serializable class. * Throws UnsupportedOperationException if this class descriptor is not * associated with a class, or if the class is externalizable, * non-serializable or does not define writeObject. */ voidinvokeWriteObject(Object obj, ObjectOutputStream out) { writeObjectMethod.invoke(obj, newObject[]{ out }); } }
AC ED 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65 73 74 05 52 81 5A AC 66 02 F6 02 00 02 49 00 07 76 65 72 73 69 6F 6E 4C 00 03 63 6F 6E 74 00 09 4C 63 6F 6E 74 61 69 6E 3B 78 72 00 06 70 61 72 65 6E 74 0E DB D2 BD 85 EE 63 7A 02 00 01 49 00 0D 70 61 72 65 6E 74 56 65 72 73 69 6F 6E 78 70 00 00 00 0A 00 00 00 42 73 72 00 07 63 6F 6E 74 61 69 6E FC BB E6 0E FB CB 60 C7 02 00 01 49 00 0E 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E 78 70 00 00 00 0B
AC ED 序列化协议标识
00 05 流版本号
73 表示这是一个新对象
72 表示这是一个新的类
readResolve() 方法
For Serializable and Externalizable classes, the readResolve method allows a class to replace/resolve the object read from the stream before it is returned to the caller.
可以在单例模式中使用:
1 2 3 4 5 6 7 8
publicclassSingleton{ ... private Object readResolve()throws ObjectStreamException { // instead of the object we're on, // return the class variable INSTANCE return INSTANCE; } }
java.lang.String.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14
publicstaticfinal Comparator<String> CASE_INSENSITIVE_ORDER = newCaseInsensitiveComparator(); privatestaticclassCaseInsensitiveComparator implementsComparator<String>, java.io.Serializable { // use serialVersionUID from JDK 1.2.2 for interoperability privatestaticfinallongserialVersionUID=8575799808933029326L;