Have you ever use serialization? Why and when is serialization required? Serialization is the process of storing an object's full state to a sequence of bytes. By deserializing the object you rebuild those bytes into a original object.
The Java Serialization API provides not only the interfaces and utilities to handle this process. Enterprise applications have distributed components in current systems. If you want to make inter-component or inter-system comunication, serialization shoul be the right choice for comparision to XML transfer.
How to make the objects of some class to be serializable? To serialize an object, you need to ensure that the class of the object implements the java.io.Serializable interface. The Serializable interface declares no methods at all. It tells the serialization mechanism that the class can be serialized.
Serialization is done by calling the writeObject() method of the java.io.ObjectOutputStream class. The object's restoration occurs with the in.readObject() method call. This method call reads in the raw bytes that we previously saved and creates a live fresh object, an exact replica of the original object.
FileInputStream fis = new FileInputStream("temp.out");
ObjectInputStream in = new ObjectInputStream(fis);
Entity entity = (Entity) in.readObject();
System.out.println(entity);
}
This is a basic knowledge of how to serialize an object. But how does the process exactly work? In general the serialization algorithm does the following:
The Java Serialization API provides not only the interfaces and utilities to handle this process. Enterprise applications have distributed components in current systems. If you want to make inter-component or inter-system comunication, serialization shoul be the right choice for comparision to XML transfer.
How to make the objects of some class to be serializable? To serialize an object, you need to ensure that the class of the object implements the java.io.Serializable interface. The Serializable interface declares no methods at all. It tells the serialization mechanism that the class can be serialized.
class Entity implements java.io.Serializable {
int someAttribute;
}
Serialization is done by calling the writeObject() method of the java.io.ObjectOutputStream class. The object's restoration occurs with the in.readObject() method call. This method call reads in the raw bytes that we previously saved and creates a live fresh object, an exact replica of the original object.
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("a.out");
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(new Entity());
os.close();
FileInputStream fis = new FileInputStream("temp.out");
ObjectInputStream in = new ObjectInputStream(fis);
Entity entity = (Entity) in.readObject();
System.out.println(entity);
}
This is a basic knowledge of how to serialize an object. But how does the process exactly work? In general the serialization algorithm does the following:
- writes out the metadata of the class associated with an object instance
- recursively writes out the description of the superclass until it finds java.lang.Object
- Once it finishes writing the metadata information, then starts with the actual attributes associated with the instance, but this time, it starts from the topmost superclass
- recursively writes the data associated with the instance, starting from the least superclass to the most-derived class