Java Serialization)
Serialization is a process of describing objects in a series of bytes. Deserialization is a process of recreating these bytes into an object. [Back-and-forth conversion of byte streams]
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.
After converting the object state into word throttling, you can use java. the classes of various byte streams in the io package save them to files, and pipe them to another thread or send object data to another host through network connection. Object serialization is simple and powerful, and has applications in RMI, Socket, JMS, and EJB. Object serialization is not the core issue in network programming, but it is very important and has many practical significance.
Java object serialization not only retains the data of an object, but also recursively stores the data of each object referenced by the object. The entire Object layer can be written to the byte stream, stored in a file or transmitted over a network connection. Object serialization allows you to perform "Deep replication" on objects, that is, copying objects and referenced objects. Serialization of an object may result in a sequence of the entire 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. Use ObjectInputStream and ObjectOutputStream to read and write objects.
public class Student implements Serializable { private static final long serialVersionUID = 1L; public int id; public String name; public ObjectSer(int id, String name) { this.id = id; this.name = name; } public static void main(String args[]) throws Exception { String file = student.ser; // Serializable ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file)); outputStream.writeObject(new Student(123, Jacky)); outputStream.close(); // Deserializable ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file)); System.out.println(inputStream.readObject()); inputStream.close(); } }The output file "student. ser" is in binary format, which is equivalent to converting the Student object into a binary string. In addition, we found that Student has only a few attributes, but the amount of information in the file is not small, because in addition to the data content, it also contains the description of the object structure.
I use EditPlus hex to view "student. ser ". 8 bits represent a byte, so two hexadecimal numbers represent a byte, as shown below:
Ac ed 00 05 73 72 00 24 63 6F 6D 2E 6A 73 63 61
69 2E 6A 61 76 61 2E 73 65 72 69 61 6C 69 7A 61
74 69 6F 6E 2E 53 74 75 64 65 6E 74 00 00 00
00 00 00 01 02 0002 49 00 02 6964 4C 00 04 6E
61 6D 65 74 00 12 4C 6A 61 76 61 2F 6C 616E 67
2F 53 74 72 69 6E 67 3B 78 7000 00 00 7B74 00
05 4A 61 63 6B 79
For the start part, see color:
Ac ed: STREAM_MAGIC. the serialization protocol is used.
00 05: STREAM_VERSION. serialization Protocol version.
73: TC_OBJECT. declare that this is a new object.
Next, output the description of the Student class, which is the first step of the serialization class. See color:
72: TC_CLASSDESC. declare that a new Class starts here.
00 24: the length of the Class name.0x24(36) bytes indicate the full name of the class:Com. jscai. java. serialization. Student
Next, the serialization information, see the color:
00 00 0000 00 00 00 01: serialVersionUID = 1L
02: serialization is supported.
Field information, see color and color:
00 02: the class has two attributes.
49: Type int; 00 02: attribute name length 2; 69 64: attribute name id.
4C: Type String; 00 04: attribute name length 4; 6E 61 6D 65: name.
74: TC_STRING. indicates a new String. the String is used to reference the object.
00 12: The length is 0x12 (18), indicating the type.Ljava/lang/String
The class Information Description ends. For details, see 7870:
78: TC_ENDBLOCKDATA indicates the end of the object block.
70: TC_NULL, indicating that there are no other superclasses.
Finally, the field attribute value is shown in the color:
00 00 007B: int occupies 4 bytes, corresponding to the first attribute, indicating id = 123.
74: TC_STRING; 00 05: Length: 5; 4A61 63 6B 79: Content: Jacky
[For details, see java. io. ObjectStreamConstants, which contains all types of codes. The String representation method is a typical example. First, it is a type identifier followed by the length and content to let JDK know the conversion policy and read length. JDK Object serialization policy. For details, refer to my "Java serialization (2)-writeObject/readObject".