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. " convert bytes to and fro "
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.
After you convert an object's state to a byte stream, you can save it to a file with a class of various byte streams in the Java.io package, pipe to another thread, or send the object data to another host over a network connection. The object serialization feature is simple and powerful, with applications in RMI, sockets, JMS, and EJBs. The problem of object serialization is not the core issue in network programming, but it is very important and has many practical meanings.
Java object serialization preserves not only the data of an object, but also the data of each object referenced by the object. You can write the entire object hierarchy to a stream of bytes that can be saved in a file or passed on a network connection. Object serialization allows you to " deep copy " The object, which is to copy the object itself and the referenced object itself. Serializing an object may get 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 token. The class with this tag 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, equivalent to converting the student object into a binary string. And we found that in fact there are only a few attributes in the student, but the amount of information in the file is not small, because in addition to the data content, it also contains the object structure of the description.
I see "Student.ser" by EditPlus Hex. The 8-bit represents a byte, so the two-digit hexadecimal number represents a byte, as follows:
AC ED xx 6F 6D 2E 6A
2E 6A All-in-2E 61
6F 6E 2E , all in all, 6E xx xx
XX 0002 6964 4C 6E
6D 4C 6A, 2F 6C 616E
2F, 6E, 3B About XX 7B The " xx "
4 a 6B 79
At the beginning, see color :
AC Ed:stream_magic. The declaration uses a serialization protocol.
XX 05:stream_version. The serialization protocol version.
73:tc_object. Declares this to be a new object.
Next output the description of the Student class, which is the first step in the serialization class, see color :
72:tc_classdesc. Declare here to start a new Class.
XX 24:class The length of the name, so the next 0x24(that is, 36) byte is the name of the class:com.jscai.java.serialization.Student
Next is the serialized information, see color :
xx 0000 xx xx 01:serialversionuid=1l
02: Serialization is supported.
field information, see colors and colors :
00 02: There are 2 properties in a class.
49: Type int; 00 02: Property name length 2; 69 64: Property name ID.
4C: type string; 00 04: Property name length 4; 6E 6D 65:name.
74:tc_string. Represents a new String. Use String to refer to the object.
00 12: Length is 0x12 (i.e. 18), indicating type ljava/lang/string
End of class information description, see 7870:
78:tc_endblockdata, the object block end of the flag.
70:tc_null, stating that there are no other super-class flags.
Finally, the attribute value of field, see color :
The 007b:int account is 4 bytes, corresponding to the first attribute, which represents id=123.
74:tc_string;00 05: Length of 5; 4a61 6B 79: Content is Jacky
"Specifically visible java.io. objectstreamconstants, which contains all the type codes. String notation is a typical example of a type identifier, followed by length and content, so that the JDK knows the conversion strategy and the length of the read. The serialization strategy of the JDK object can refer to my " preliminary discussion of Java Serialization (2)-writeobject/readobject"
Refer to Http://longdick.iteye.com's blog post
Preliminary discussion of Java Serialization (serialization)