Java Serialization (serialization) mechanism _java

Source: Internet
Author: User

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.

After you convert an object state to a byte stream, you can save it to a file with a class of java.io in the package, pipe to another thread, or send the object data to another host over a network connection. Object serialization is very simple and powerful, and is used in RMI, sockets, JMS, and EJBs. The problem of object serialization is not the core topic in network programming, but it is very important and has many practical meanings.

Java object serialization not only preserves the data of one object, but also recursively holds the data for each object referenced by the object. The entire object hierarchy can be written to a byte stream and can be saved in a file or passed on a network connection. Object serialization enables the "deep copy" of objects, that is, the copying of objects themselves and the objects they refer to. Serializing an object can get a sequence of the entire object.

Basic methods of Use:

Serialization refers to persisting a class or a basic data type (persistence) to a data stream (stream), including file, byte stream, and network data flow.

Java implementation of serialization mainly rely on two classes: Objectouputstream and ObjectInputStream. They are subclasses of OutputStream and InputStream in the Java IO System. Since they are streams in Java IO, they can be manipulated as if they were a normal stream. Here's how they use it:

Import Java.io.ByteArrayInputStream; 
Import Java.io.ByteArrayOutputStream; 
Import java.io.IOException; 
Import Java.io.ObjectInputStream; 
Import Java.io.ObjectOutputStream; 
 
Import java.io.Serializable; 
 public class Pair implements serializable{private static final long serialversionuid = -1874850715617681161l; 
 private int type; 
 
 private String name; 
 public int GetType () {return type; 
 public void SetType (int type) {this.type = type; 
 Public String GetName () {return name; 
 public void SetName (String name) {this.name = name; 
 public Pair (int type, String name) {super (); 
 This.type = type; 
 THIS.name = name; public static void Main (string[] args) throws IOException, ClassNotFoundException {//TODO auto-generated method 
 Stub//serialize Object pair Bytearrayoutputstream bos = new Bytearrayoutputstream (); 
 ObjectOutputStream oos = new ObjectOutputStream (BOS); 
 Pair Pair = new Pair (1, "Charlie"); Oos.writeoBject (pair); 
 Deserialize object, get new Object Newpair Bytearrayinputstream bis = new Bytearrayinputstream (Bos.tobytearray ()); 
 ObjectInputStream ois = new ObjectInputStream (bis); 
 
 Pair Newpair = (Pair) ois.readobject (); 
 System.out.println (Newpair.gettype () + ":" +newpair.getname ());  } 
}

1. These two classes are all decorator patterns, and when they are created, they are passed in a byte-based stream, where the serialized data is actually stored.

2. The persisted class implements the serializable interface, which does not have any functions, but is a markup interface. If serializing on one machine, transferring the resulting data to another machine for deserialization, the classes on both machines should be exactly the same, otherwise serialization would not be successful.

3. Remember not to use the BOS in the code above to get string, then get Bytearrayinputstream from this string, and then deserialize it. The bos is stored in bytes, and a string that is stored as a character is bound to result in a change in the data, and byte[from a string will not be the previous byte[]. I've had this problem because I want to have the serialized data in an XML file. The specific solution to this problem see my other article:

Www.jb51.net/article/88130.htm

What do Java virtual machines do when serializing and deserializing?

In Javadoc, the Java serialization mechanism is described in detail in the descriptions of these two classes:

Reference

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of a ll non-transient and Non-static fields. References to the other objects (except in transient or static fields) cause those the to is objects written. Multiple references to a single object are encoded using a reference sharing mechanism so-graphs of objects can be re stored to the same shape as when the original is written.

The default serialization mechanism writes to the data in the stream:

1, the object belongs to the class
2, the class's signature
3. All non-transient and non-static properties
4. References to other objects can also cause serialization of these objects
5. If multiple references point to an object, then the sharing reference mechanism is used

Reference

Classes that require special handling during the serialization and deserialization process must implement special methods With these exact signatures:

private void ReadObject (Java.io.ObjectInputStream stream) 
 throws IOException, ClassNotFoundException; 
private void WriteObject (Java.io.ObjectOutputStream stream) 
 throws IOException 
private void Readobjectnodata () 
 

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.