Java Object Serialization Usage basics

Source: Internet
Author: User

Object serialization is the conversion of the state of an object into a byte stream, which can then be used to generate objects of the same state. This process can also be done over the network by first creating an object on the Windows machine, serializing it, and then sending it over the network to a UNIX machine, where it can be correctly reassembled. One of them, like RMI, sockets, JMS, EJB, and why they can pass Java objects, is, of course, the credit of the object serialization mechanism.

The Java object Serialization mechanism generally has two uses:

The state information of the Java Javabeans:bean is usually configured at design time, and the bean's state information must be saved so that the state information can be restored when the program is run, which requires that the state of the object be saved to a file, and then the object can be reconstructed by reading the object state. Restore program status.

RMI allows you to manipulate objects on a remote machine as you do on the local computer, or programs that use sockets to transfer objects over a network, all of which need to implement the Serializaiton mechanism.

We can serialize the class by having the class implement the Java.io.Serializable interface. This interface is a manufacturer (marker) interface. That is, the interface does not need to implement any methods for the class to implement it. It is used primarily to inform the Java Virtual Machine (JVM) of the need to serialize an object.

For this, there are several points we need to be clear:

Not all classes can be serialized, under CMD, we enter Serialver Java.net.Socket, we can get the information of whether the socket can be serialized, in fact, the socket is not serializable.

Java has many basic classes that have implemented serializable interfaces, such as String,vector. However, for example, Hashtable does not implement the serializable interface.

There are two main classes that read or write to the object: ObjectOutputStream and ObjectInputStream. ObjectOutputStream provides a writeobject method for writing an object to an output stream, ObjectInputStream provides a readobject method for reading an object from an input stream. Objects that use these methods must already be serialized. In other words, the serializable interface must already be implemented. If you want to writeobject a Hashtable object, you get an exception.

The process of serialization is the object writes the byte stream and reads the object from the byte stream. After you convert an object state to a byte stream, you can save it to a file with a variety of byte streams in the Java.io 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 most exciting subject in network programming, but it is very important and has many practical meanings.

Object serialization enables the implementation of distributed objects. Main applications For example: RMI uses object serialization to run a service on a remote host as if it were running on a local machine.

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 the entire sequence of objects.

Java serialization is simpler and usually requires no custom code to save and recover the state of the object. A class object that implements the Java.io.Serializable interface can be converted to a byte stream or recovered from a byte stream, without adding any code to the class. In rare cases, custom code is required to save or restore object state. Note here: Not every class is serializable, and some classes are not serializable, for example, a class involving threads has a very complex relationship with a particular JVM.

Serialization mechanism:

Serialization is divided into two parts: serialization and deserialization. Serialization is the first part of the process that decomposes data into byte streams for storage in files or on the network. Deserialization is the opening of a byte stream and the construction of an object. Object serialization not only converts basic data types to byte representations, but sometimes restores data. Recovery data requires an object instance that recovers data. The serialization process in ObjectOutputStream is connected to a byte stream, including the object type and version information. When deserialized, the JVM generates an object instance with the header information, and then copies the data in the object byte stream to the object data member. Here are two major sections to illustrate:

To process an object stream:

(Serialization process and deserialization process)

The Java.io package has two classes of serialized objects. ObjectOutputStream is responsible for writing the object to the byte stream and ObjectInputStream the object from the byte stream.

Let's get to the ObjectOutputStream class first. The ObjectOutputStream class extends the DataOutput interface.

The WriteObject () method is the most important method for object serialization. If the object contains references to other objects, the WriteObject () method recursively serializes the objects. Each ObjectOutputStream maintains a serialized object reference table, preventing multiple copies of the same object from being sent. (This is important) because WriteObject () can serialize an entire set of cross-referenced objects, the same ObjectOutputStream instance may be accidentally requested to serialize the same object. In this case, the deserialization is serialized instead of the object byte stream being written again.

Let's take a look at the ObjectOutputStream class from the examples below.

// 序列化 today's date 到一个文件中. 
FileOutputStream  f =new  FileOutputStream ("tmp" );
ObjectOutputStream  s =new  ObjectOutputStream (f);
s.writeObject("Today" );
s.writeObject(new Date());
s.flush(); 

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.