Java serialization mechanisms and principles

Source: Internet
Author: User
Tags object serialization

Java The analysis of serialization algorithm

Serialization (serialization) is a process of describing objects in a sequence of bytes, and deserializing deserialization is a process of re-building 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 to serialize and the Java serialization algorithm, and we use an instance to demonstrate how the serialized bytes describe the information of an object.

The necessity of serialization

In Java, everything is an object, and in a distributed environment it is often necessary to pass an 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, the serializable interface has no method, more like a token.
The class with this tag can be processed by the serialization mechanism.

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

Then we write a program that serializes and outputs the object. The ObjectOutputStream can output an object as a byte stream.
We temporarily store the byte stream in the Temp.out file.

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 ();

}
If you want to read the bytes rebuild object from a persisted file, we can use ObjectInputStream.  
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);
Serialization format of the object
What does it look like after serializing an object? Open the Temp.out file that we just serialized the object out of.
AC ED (0A), 6C 6573 A0 0C 0563 B1 DD F9, 76, 6, 07, 6E,, 74 42 00 5 6F 6E 7870 00 64
This lump of bytes is used to describe the Testserial object after serialization, and we note that there are only two domains in the Testserial class:
Public byte version = 100;
Public byte count = 0;
And both are byte types, which theoretically store only 2 bytes for both domains, but in fact temp.out occupy 51bytes, meaning that in addition to the data, other descriptions of the serialized object are included
Java the serialization algorithm
The serialization algorithm typically does the following steps as follows:
Outputs the class metadata related to the object instance.
Recursively outputs a superclass description of a class until there are no more super-classes.
After the class metadata is finished, start outputting the actual data values of the object instances from the topmost superclass.
Recursive output of data from top to bottom instances
We illustrate with another example that covers all possible situations more fully:
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 ();
}
}
      1. AC Ed:stream_magic. The declaration uses a serialization protocol.
      2. XX 05:stream_version. The serialization protocol version.
      3. 0x73:tc_object. Declares this to be a new object.
      1. 0x72:tc_classdesc. Declare here to start a new class.
      2. The length of the 0a:class name of the XX.
      3. The 6c of the 74:serialtest,class class name.
      4. 5A AC F6:serialversionuid, serialized ID, if not specified,
        A 8byte ID is randomly generated by the algorithm.
      5. 0X02: Tag number. This value declares that the object supports serialization.
      6. 00 02: The number of fields that the class contains.
      1. 0x49: Domain type. 49 means "I", which is int.
      2. 00 07: The length of the domain name Word.
      3. 6F 6e:version, domain name word description.
      1. 0x4C: The type of the domain.
      2. 00 03: Domain name word length.
      3. 6F 6E: Domain name word description, con
      4. 0x74:tc_string. Represents a new string. The object is referenced by a string.
      5. 00 09: The string length.
      6. 4C 6F 6E 3b:lcontain (6E), standard object signature notation for the JVM.
      7. 0x78:tc_endblockdata, object data block end flag
      1. 0x72:tc_classdesc. Declare that this is a new class.
      2. 00 06: Class name length.
      3. 6E 74:parent, class name description.
      4. 0E DB D2 BD-EE 7a:serialversionuid, serialized ID.
      5. 0X02: Tag number. This value declares that the object supports serialization.
      6. 00 01: The number of fields in the class.
      1. 0x49: Domain type. 49 means "I", which is int.
      2. 0 d: Domain name word length.
      3. The 6E, 6F 6e:parentversion, the domain name word description.
      4. 0x78:tc_endblockdata, the object block end of the flag.
      5. 0x70:tc_null, stating that there are no other super-class flags:
      1. XX 0a:10, parentversion field value.
      1. XX 42:66, the value of the Version field.
      1. 0x73:tc_object, declares that this is a new object.
      2. 0x72:tc_classdesc declares a new class to start here.
      3. 00 07: The length of the class name.
      4. 6F 6E 6e:contain, class name description.
      5. FC BB E6 0E FB CB c7:serialversionuid, serialized ID.
      6. 0x02:various flags. The tag number. This value declares that the object supports serialization
      7. 00 01: The number of fields within the class.
      1. 0x49: Domain type. 49 means "I", which is int.
      2. 0E: Domain name word length.
      3. 6F 6E---6E-------------------6e:containversion
      4. A flag that 0x78:tc_endblockdata the end of the object block.
      1. 0x70:tc_null, there's no super class.
      1. XX 0b:11, containversion value.

    This example is quite straightforward. The Serialtest class implements the parent superclass and also holds a container object inside.

    The serialized format is as follows:

    AC ED (0A) 6C 54 65

    5A AC F6 02 00 02 49 00 07

    6F 6E 4C 74 00 09 6F 6E

    4C 6F 6E 78 72 00 06 70 61 72

    6E 0E DB D2 BD-EE 02 7A 00 01 49 00

    0D, 6E, 78, 6F, 6E, 70

    0A, XX, 6F 6E 74.

    6E FC BB E6 0E FB CB C7 02 00 01 49 00

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

    XX 0B

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

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

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

    The algorithm then outputs the next field, contain con = new contain (); This is a bit special, it's an object.
    The standard object signature notation that describes the use of the JVM when describing object type references, see color:

    The next algorithm will output the superclass, which is the parent class description, see color:

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

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

    There are also domains for the Serialtest class:

    The next bytes more interesting, the algorithm needs to describe the contain class of information, to remember,
    The contain class has not been described yet, see color:

    . The only domain description of the output contain, int containversion=11;

    At this point, the serialization algorithm checks to see if the contain has a superclass and then outputs if any.

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

    OK, we discussed the mechanism and principle of the Java serialization, hoping to help the students.

Transfer from http://www.java3z.com/cwbwebhome/article/article8/862.html important role of Serialversionuid value           Based on the above analysis, it can be found that if a class can be serialized, Serialversionuid suggests a definite value, not automatically generated by the system, otherwise, if the field type and length cannot be modified, the deserialization will fail if the different versions of the classes on both sides of the class. Attention IssuesIf the code is written like this when serializing:

Oos.writeobject ((parent) ST);
It is found that the serialized object is still serialtest, and if it is deserialized with the parent in a distributed environment (the call segment does not exist serialtest), it will cause classnotfoundexception.

Java serialization mechanisms and principles

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.