With JavaTM remote method invocation (RMI), distributed object-based applications can be easily developed. The simplicity of RMI is the cost of network traffic. The underlying sockets can be used to develop client/server systems, but because most Java I/O classes and objects are not easily matched, how do you pass the finished object through the socket? Object serialization is a mechanism that allows you to read/write completed objects in a bit-stream fashion.
By combining the underlying sockets and object serialization, you will get a powerful, efficient, and alternative RMI mechanism by which passing objects through sockets can also overcome the high cost of using RMI.
Overview of Object serialization
The object serialization mechanism is useful for programs that need to save the state of an object to a file and then reconstruct the object by reading the state of the object, restoring the program state, or using the socket to transfer objects over the network. You can serialize a 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.
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. It is important to note that objects using these methods must be serialized. In other words, these classes must implement the Serializable interface.
Serialization of already existing classes
After understanding the basics of object serialization, let's look at how to stream a read/write object or an existing serialized class instance. To write an object to the output stream, create an output stream, and then use the WriteObject object to save to the file.
--------------------------------------------------------------------------------
Note: The date class is serializable. In other words, it implements the serializable interface.
--------------------------------------------------------------------------------
Routine 1:savedate.java
import java.io.*;
import java.util.Date;
public class SaveDate {
public static void main(String argv[]) throws Exception {
FileOutputStream fos = new FileOutputStream("date.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Date date = new Date();
oos.writeObject(date);
oos.flush();
oos.close();
fos.close();
}
}
It is easy to read the object and reconstruct its state. The code in routine 2 shows you how to read a serialized object and print its information.
Routine 2:readdate.java
import java.io.*;
import java.util.Date;
public class ReadDate {
public static void main(String argv[]) throws Exception {
FileInputStream fis = new FileInputStream("date.out");
ObjectInputStream ois = new ObjectInputStream(fis);
Date date = (Date) ois.readObject();
System.out.println("The date is: "+date);
ois.close();
fis.close();
}
}