Deep analysis of the mechanism and principle of Java serialization _java

Source: Internet
Author: User
Tags object serialization serialization

Analysis of Java serialization algorithm

Serialization (serialization) is a process of describing an object in a sequence of bytes; Deserialization deserialization is the process of resetting these bytes into an object. The Java Serialization API provides a standard mechanism for handling object serialization. Here you can learn how to serialize an object, when it needs serialization, and Java serialization algorithms, and we use an example to demonstrate how the serialized bytes describe an object's information.
The necessity of serialization

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

The premise that an object can be serialized is to implement the serializable interface, and the serializable interface has no method, more like a tag. The class with this tag can be processed by the serialization mechanism.

Copy Code code as follows:

Import java.io.Serializable;
Class Testserial implements Serializable {
Public byte version = 100;
Public byte count = 0;
}

Then we write a program to serialize and output the object. ObjectOutputStream can output object as a byte stream. We temporarily store the byte stream in the Temp.out file.
Copy Code code 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 the bytes Rebuild object from a persistent file, we can use ObjectInputStream.
Copy Code code as follows:

public static void Main (String args[]) throws IOException {
FileInputStream fis = new FileInputStream ("Temp.out");
ObjectInputStream oin = new ObjectInputStream (FIS);
Testserial ts = (testserial) oin.readobject ();
System.out.println ("version=" +ts.version);
}

Execution results are

100.
Serialization format for objects

What does it look like after serializing an object? Open the Temp.out file that we just serialized the object output in a 16-way display. The content should read as follows:

Copy Code code as follows:

AC ED 0A 54 65 (6C)

A0 0C FE B1 DD F9 02 00 02 42 00 05

6F 6E A-M-a-the 6F 6E 78

70 00 64


This lump byte is used to describe the Testserial object after serialization, and we notice that there are only two domains in the Testserial class:

Public byte version = 100;

Public byte count = 0;

and are all byte, theoretically storing the two fields requires only 2 byte, but in fact the Temp.out occupies 51bytes, which means that other descriptions of the serialized object are included in addition to the data.
Java Serialization algorithm

The serialization algorithm usually does the following things in a step:

The class metadata output that is associated with an object instance.

Recursively output the superclass description of the class until there is no more superclass.

When the class metadata is finished, the actual data value of the object instance is started out from the topmost superclass.

Recursively outputs the data of an instance from top to bottom

We use another example that covers all possible situations with a more complete description:

Copy Code code 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.Serialtestclass implementsHadParentSuper class, the interior also holds aContainerObject.

The serialized format is as follows:

AC ED 0A (6C ), and

F6 ba 5A AC-yi-June

A (6F 6E 4C) 6F 6E

4C 6F 6E mb/6E 3B to M.

6E 0E DB D2 BD EE (7A)

0D to 6E and 78 70 for the 6F 6E

0A A ( 6F) 6E(a) /c8>

6E FC BB E6 0E FB CB C7

0E 6F 6E--6E--78

0B

Let's take a closer look at what these bytes mean. In the beginning, see color:

    1. AC Ed:stream_magic. The declaration uses a serialization protocol .
    2. 05:stream_version. The serialization protocol version .
    3. 0x73:tc_object.  Declare this to be a new object .  

The first step in the serialization algorithm is the description of the output object-related class. The object shown in the example is the Serialtest class instance, so the description of the Serialtest class is then output. See color:

    1. 0x72:tc_classdesc. &NBSP declares that a new Class is started here.
    2. 0a:class length of name .
    3. 6c:   Serialtest,class class name .
    4. 5A/AC F6:   serialversionuid,   serialization id , if not specified, the algorithm randomly generates a 8byte ID.
    5. 0x02:   Tag number . &NBSP The value declares that the object supports serialization.
      ,
    6. :   the number of fields that the class contains.

Next, the algorithm outputs one of the fields,int version=66; see color:

    1. 0x49: field type . The "I" is the Int.
    2. The length of the name word .
    3. 6F 6e:version, domain name Description .

Then, the algorithm outputs the next domain,contain con = new contain (); This is a little special, it's an object. When describing object type references, you need to use the JVM's standard object signature notation, see color:

    1. 0x4c:   field type .
    2. :   domain name length .
    3. 6F 6E:   Domain name description, con
    4. 0x74:tc_string.   represents a new String. Use String to refer to the object.
    5. :   span>string length .
    6. 4 c 6F 6E   6E 3 B: lcontain, JVM Standard object signature notation pan>
    7. 0x78:tc_endblockdata Object data block end flag

. The algorithm then outputs the superclass, which is described by the Parent class, see color:

    1. 0x72:tc_classdesc. declare that this is a new class .
    2. The class name length .
    3. 6E 74:parent, class name description.
    4. 0E DB D2 BD EE 7 A: serialversionuid, serialization ID.
    5. 0x02: Tag number . This value declares that the object supports serialization .
    6. Count: The number of fields in the class .

Next, output the domain description of the parent class,int parentversion=100; See color:

    1. 0x49: field type . The "I" is the Int.
    2. 0 D: The length of the word name .
    3. A 6E-A-parentversion, 6F 6E: The name of the word description.
    4. 0x78:tc_endblockdata, a flag that ends the object block.
    5. 0x70:tc_null, stating that there are no other super class flags. .

So far, the algorithm has output all the descriptions of the classes. The next step is to export the actual value of the instance object. This is the beginning of the domain from parent Class , see color:

    1. 0a:10, parentversion value of the field .

There are also domains for the Serialtest class:

    1. 42:66, value of Version field .

bytes More interesting, the algorithm needs to describe the contain class of information, to remember, there is no contain class has been described, see color:

    1. 0x73:tc_object,   Declare this is a new object . The
    2. 0x72:tc_classdesc declaration starts here with a new Class.
    3. :   class name length .
    4. 6F 6E 6e:contain, class name Description .
    5. FC BB E6 0E FB CB C7:   serialversionuid,   serialization id.
    6. 0x02:various flags. &NBSP Tag number . &NBSP This value declares that the object supports serialization
    7. 01: &NBSP the number of fields within the class.

. output contain 's unique domain description,int containversion=11;

    1. 0x49: field type . The "I", which is Int.
    2. 0E: The length of the word name .
    3. 6F 6E MB-6E-A-containversion 6F 6E: name Description .
    4. the flag that 0x78:tc_endblockdata the end of the object block .

At this point, the serialization algorithm checks whether the contain has a superclass and, if so, then outputs it.

    1. 0x70:tc_null, there's no super class.

Finally, the actual field value of the contain class is output.

    1. 0b:11, containversion value .

OK, We discussed the Java Serialization mechanism and principle, hope to be able to help students.

Related Article

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.