In-depth analysis of Java serialization mechanism and Principle

Source: Internet
Author: User

Java serialization algorithm analysis

Serialization is a process of describing objects in a series of bytes. deserialization is a process of recreating these bytes into an object. Java serialization API provides a standard mechanism for processing object serialization. Here you can learn how to serialize an object, when to serialize It, And the Java serialization algorithm. We use an instance to demonstrate how the serialized bytes describe the information of an object.
Serialization necessity

In Java, everything is an Object. In a distributed environment, it is often necessary to pass an Object from this network or device to the other end. This requires a protocol that can transmit data at both ends. Java serialization mechanism is generated to solve this problem.
How to serialize an object

The premise that an object can be serialized is to implement the Serializable interface. The Serializable interface has no method, more like a flag. The Class with this mark can be processed by the serialization mechanism.Copy codeThe Code is as follows: import java. io. Serializable;
Class TestSerial implements Serializable {
Public byte versions = 100;
Public byte count = 0;
}

Then we write a program to serialize and output the object. ObjectOutputStream can output an Object as a Byte stream. We temporarily store the Byte stream to the temp. out file.Copy codeThe Code is as follows: public static void main (String args []) throws IOException {
FileOutputStream fos = new FileOutputStream ("temp. out ");
ObjectOutputStream oos = new ObjectOutputStream (fos );
TestSerial ts = new TestSerial ();
Oos. writeObject (ts );
Oos. flush ();
Oos. close ();
}

If you want to read Bytes from a persistent object and recreate the object, you can use ObjectInputStream.Copy codeThe Code is as follows: public static void main (String args []) throws IOException {
FileInputStream FCM = new FileInputStream ("temp. out ");
ObjectInputStream oin = new ObjectInputStream (FCM );
TestSerial ts = (TestSerial) oin. readObject ();
System. out. println ("version =" + ts. version );
}

The execution result is

100.
Object serialization format

What does serialization look like? Open the temp. out file that we serialized and output the object, and display it in hexadecimal format. The content should be as follows:Copy codeCode: ac ed 00 05 73 72 00 0A 53 65 72 69 61 6C 54 65

73 74 A0 0C 34 00 FE B1 DD F9 02 00 02 42 00 05

63 6F 75 6E 74 42 00 07 76 65 72 73 69 6F 6E 78

70 00 64

This lump of bytes is used to describe the serialized TestSerial object. We noticed that the TestSerial class has only two fields:

Public byte versions = 100;

Public byte count = 0;

Both are in the byte type. Theoretically, only two bytes are required for storing these two fields, but in fact temp. out occupies 51 bytes of space, that is, in addition to data, it also includes other descriptions of serialized objects.
Java serialization Algorithm

Serialization algorithms generally follow the steps below:

◆ Output class metadata related to the object instance.

◆ Recursively output the superclass description of the class until there are no superclasses.

◆ After the class metadata is complete, the actual data value of the object instance starts to be output from the top-level superclass.

◆ Recursively output instance data from top to bottom

We use another example that covers all possible situations in a more complete way:Copy codeThe Code is as follows: class parent implements Serializable {
Int parentVersion = 10;
}

Class contain implements Serializable {
Int containVersion = 11;
}

Public class SerialTest extends parent implements Serializable {
Int version = 66;
Contain con = new contain ();
Public int getVersion (){
Return version;
}
Public static void main (String args []) throws IOException {
FileOutputStream fos = new FileOutputStream ("temp. out ");
ObjectOutputStream oos = new ObjectOutputStream (fos );
SerialTest st = new SerialTest ();
Oos. writeObject (st );
Oos. flush ();
Oos. close ();
}
}

This example is quite straightforward. The SerialTest class implements the Parent superclass and also holds a Container object internally.

The serialized format is as follows:

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

Let's take a closer look at what these bytes represent. For the start part, see color:

  1. Ac ed: STREAM_MAGIC. the serialization protocol is used.
  2. 00 05: STREAM_VERSION. serialization Protocol version.
  3. 0x73: TC_OBJECT. declare that this is a new object.

The first step of the serialization algorithm is to describe the class related to the output object. The object shown in the example is a SerialTest class instance. Therefore, the description of the SerialTest class is output. See color:

  1. 0x72: TC_CLASSDESC. declare that a new Class starts here.
  2. 00 0A: length of the Class name.
  3. 53 65 72 69 61 6c 54 65 73 74: SerialTest, Class name.
  4. 05 52 81 5A AC 66 02 F6: SerialVersionUID, serialization ID. If not specified, an 8-byte ID will be randomly generated by the algorithm.
  5. 0x02: Mark number. This value declares that this object supports serialization.
  6. 00 02: number of fields contained in the class.

Next, the algorithm outputs a field, int version = 66; see color:

  1. 0x49: The field type. 49 indicates "I", that is, Int.
  2. 00 07: the length of the domain name.
  3. 76 65 72 73 69 6F 6E: version, Domain Name Description.

Then, the algorithm outputs the next field, contain con = new contain (); which is a special object. The standard JVM object signature representation is required to describe object type reference. See color:

  1. 0x4C: the type of the domain.
  2. 00 03: Domain Name Length.
  3. 63 6F 6E: Domain Name Description, con
  4. 0x74: TC_STRING. represents a new String. the String is used to reference the object.
  5. 00 09: length of the String.
  6. 4C 63 6F 6E 74 61 69 6E 3B: Lcontain;, JVM Standard Object signature notation.
  7. 0x78: TC_ENDBLOCKDATA, mark of the end of the object data block

Next, the algorithm will output the super class, that is, the description of the Parent class. See the color:

  1. 0x72: TC_CLASSDESC. declare that this is a new class.
  2. 00 06: Class Name Length.
  3. 70 61 72 65 6E 74: parent, Class Name Description.
  4. 0E DB D2 BD 85 EE 63 7A: SerialVersionUID, serialization ID.
  5. 0x02: Mark number. This value declares that this object supports serialization.
  6. 00 01: number of fields in the class.

Next, output the domain description of the parent class, int parentVersion = 100; see the color in the same way:

  1. 0x49: The field type. 49 indicates "I", that is, Int.
  2. 00 0D: Domain Name Length.
  3. 70 61 72 65 6E 74 56 65 72 73 69 6F 6E: parentVersion, Domain Name Description.
  4. 0x78: TC_ENDBLOCKDATA, the identifier of the end of the object block.
  5. 0x70: TC_NULL, indicating that there are no other superclasses ..

So far, the algorithm has output descriptions of all classes. The next step is to output the actual value of the Instance Object. At this time, it starts from the parent Class field. See the color:

  1. 00 00 00 0A: 10, parentVersion field value.

There are also SerialTest class fields:

  1. 00 00 00 42: 66, the value of the version field.

Next, bytes is interesting. Algorithms need to describe the information of the contain Class. Remember that the contain Class has not been described yet. See the color:

  1. 0x73: TC_OBJECT, declares that this is a new object.
  2. 0x72: TC_CLASSDESC declares that a new Class starts here.
  3. 00 07: length of the class name.
  4. 63 6F 6E 74 61 69 6E: contain, Class Name Description.
  5. Fc bb E6 0E fb cb 60 C7: SerialVersionUID, serialization ID.
  6. 0x02: Various flags. Flag number. This value declares that this object supports serialization.
  7. 00 01: number of fields in the class.

. Output the unique domain description of contain, int containVersion = 11;

  1. 0x49: The field type. 49 indicates "I", that is, Int ..
  2. 00 0E: Domain Name Length.
  3. 63 6F 6E 74 61 69 6E 56 65 72 73 69 6F 6E: containVersion, Domain Name Description.
  4. 0x78: indicates the end of The TC_ENDBLOCKDATA object block.

In this case, the serialization algorithm checks whether there are superclasses in the contain, and if so, the output is continued.

  1. 0x70: TC_NULL, no superclass.

Finally, output the actual Domain value of the In in class.

  1. 00 00 00 0B: 11, the value of containVersion.

Okay. We have discussed the mechanism and principle of java serialization, hoping to help the students.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.