Java Study Notes: go deep into Serializable

Source: Internet
Author: User

Java Serializable

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.

To put it bluntly, Java uses objects to describe objects, but objects cannot be transmitted over the network. If an object is generated, a series of "Byte descriptions" can be parsed ", then the object data can be transmitted on the network.


After the object is serialized, a file ending with ". out" is generated. The file is opened in hexadecimal notation as follows:

[Java]
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

A Java Serializable instance:

Java objects need to be serialized, and data structure objects need to implement the Serializable interface, for example, the following code:

[Java]
/**
* Guest_book Data Structure
* @ Author zhuli. zhul
*/
Public class GuestbookDo implements Serializable {// implements Serializable is the key

Private String title;

Private String content;

Private String username;

Private int id;


Public int getId (){
Return id;
}
 
Public void setId (int id ){
This. id = id;
}
 
Public String getTitle (){
Return title;
}
 
Public void setTitle (String title ){
This. title = title;
}
 
Public String getContent (){
Return content;
}
 
Public void setContent (String content ){
This. content = content;
}
 
Public String getUsername (){
Return username;
}
 
Public void setUsername (String username ){
This. username = username;
}
 
Public int getDtime (){
Return dtime;
}
 
Public void setDtime (int dtime ){
This. dtime = dtime;
}
 
Private int dtime;
}

The GuestbookDo object is serialized and written into the data stream through data flow. In addition, the temps. out file is generated in the web directory, which is a data stream file of the serialized object, and the object can be written into the data stream.

[Java]
@ RequestMapping (value = "/test ")
@ ResponseBody
Public String test () throws IOException {
// Object Instantiation
GuestbookDo gb = new GuestbookDo ();
Gb. setContent ("asdasd ");
Gb. setId (100 );
// Data stream
FileOutputStream fos = new FileOutputStream ("temps. out ");
ObjectOutputStream oos = new ObjectOutputStream (fos );
Oos. writeObject (gb );
Oos. flush ();
Oos. close ();
Return "test ";
}

 

We can read the object structure in the data stream by reading the temps. out method.

[Java]
@ RequestMapping (value = "/testa ")
@ ResponseBody
Public String testa () throws Exception {
InputStream is = new FileInputStream ("temps. out ");
 
 
ObjectInputStream ois = new ObjectInputStream (is );
 
GuestbookDo gb = (GuestbookDo) ois. readObject ();

System. out. println ("id =" + gb. getId ());
System. out. println ("content =" + gb. getContent ());
Ois. close ();
Return "test1 ";
}

Result:

 

Serialization algorithms generally follow the steps below:

Outputs class metadata related to the object instance.
Recursively output the superclass description of the class until there is no superclass.
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

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.