Java序列化算法实现和说明
Serialization is the process of saving an object's state to a sequence of bytes; deserialization is the process of rebuilding those bytes into a live object. The Java Serialization API provides a standard mechanism for developers to handle object serialization. In this tip, you will see how to serialize an object, and why serialization is sometimes necessary. You'll learn about the serialization algorithm used in Java, and see an example that illustrates the serialized format of an object. By the time you're done, you should have a solid knowledge of how the serialization algorithm works and what entities are serialized as part of the object at a low level.
Why is serialization required?In today's world, a typical enterprise application will have multiple components and will be distributed across various systems and networks. In Java, everything is represented as objects; if two Java components want to communicate with each other, there needs be a mechanism to exchange data. One way to achieve this is to define your own protocol and transfer an object. This means that the receiving end must know the protocol used by the sender to re-create the object, which would make it very difficult to talk to third-party components. Hence, there needs to be a generic and efficient protocol to transfer the object between components. Serialization is defined for this purpose, and Java components use this protocol to transfer objects.
Figure 1 shows a high-level view of client/server communication, where an object is transferred from the client to the server through serialization.
?

?
In order to serialize an object, you need to ensure that the class of the object implements the java.io.Serializable interface, as shown in Listing 1.
?Figure 2. An outline of the serialization algorithmLet's go through the serialized format of the object in detail and see what each byte represents. Begin with the serialization protocol information:
AC ED:STREAM_MAGIC. Specifies that this is a serialization protocol.00 05:STREAM_VERSION. The serialization version.0x73:TC_OBJECT. Specifies that this is a newObject.The first step of the serialization algorithm is to write the description of the class associated with an instance. The example serializes an object of type
SerialTest, so the algorithm starts by writing the description of theSerialTestclass.0x72:TC_CLASSDESC. Specifies that this is a new class.00 0A: Length of the class name.53 65 72 69 61 6c 54 65 73 74:SerialTest, the name of the class.05 52 81 5A AC 66 02 F6:SerialVersionUID, the serial version identifier of this class.0x02: Various flags. This particular flag says that the object supports serialization.00 02: Number of fields in this class.Next, the algorithm writes the field
int version = 66;.0x49: Field type code. 49 represents "I", which stands forInt.00 07: Length of the field name.76 65 72 73 69 6F 6E:version, the name of the field.And then the algorithm writes the next field,
contain con = new contain();. This is an object, so it will write the canonical JVM signature of this field.0x74:TC_STRING. Represents a new string.00 09: Length of the string.4C 63 6F 6E 74 61 69 6E 3B:Lcontain;, the canonical JVM signature.0x78:TC_ENDBLOCKDATA, the end of the optional block data for an object.The next step of the algorithm is to write the description of the
parentclass, which is the immediate superclass ofSerialTest.0x72:TC_CLASSDESC. Specifies that this is a new class.00 06: Length of the class name.70 61 72 65 6E 74:SerialTest, the name of the class0E DB D2 BD 85 EE 63 7A:SerialVersionUID, the serial version identifier of this class.0x02: Various flags. This flag notes that the object supports serialization.00 01: Number of fields in this class.Now the algorithm will write the field description for the
parentclass.parenthas one field,int parentVersion = 100;.0x49: Field type code. 49 represents "I", which stands forInt.00 0D: Length of the field name.70 61 72 65 6E 74 56 65 72 73 69 6F 6E:parentVersion, the name of the field.0x78:TC_ENDBLOCKDATA, the end of block data for this object.0x70:TC_NULL, which represents the fact that there are no more superclasses because we have reached the top of the class hierarchy.So far, the serialization algorithm has written the description of the class associated with the instance and all its superclasses. Next, it will write the actual data associated with the instance. It writes the parent class members first:
00 00 00 0A: 10, the value ofparentVersion.Then it moves on to
SerialTest.00 00 00 42: 66, the value ofversion.The next few bytes are interesting. The algorithm needs to write the information about the
Listing 8. The contain objectcontainobject, shown in Listing 8.Java RMI (William Grosso, O'Reilly, October 2001) is also a useful reference.?
SOURCE URL : http://www.javaworld.com/community/node/2915
好的文章就收藏一下![]()