Wednesday, 3 October 2018

Serialization

Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time. Serialization is used when you want to persist the object. It is also used by Java RMI to pass objects between JVMs, either as arguments in a method invocation from a client to a server or as return values from a method invocation, or as exceptions thrown by remote methods. In general, serialization is used when we want the object to exist beyond the lifetime of the JVM.

java.io.Serializable is a marker interface (has no body). It is just used to "mark" Java classes as serializable.The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during de-serialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named serialVersionUID that must be static, final, and of type long: <<accessSpecifier>> static final long serialVersionUID = 1L;How to make a class eligible for serializationTo persist an object the respective class must implement the java.io.Serializable interface.import java.io.*;public class SerialClass implements Serializable { private static final long serialVersionUID = 1L; private Date currentTime; public SerialClass() { currentTime = Calendar.getInstance().getTime(); } public Date getCurrentTime() { return currentTime; }}How to write an object into a fileNow we need to write this object to a file system. We use java.io.ObjectOutputStream for this purpose.

import java.io.*;

public class PersistSerialClass

{

public static void main(String [] args)

{

String filename = "time.ser";

//We will write this object to file system.

SerialClass time = new SerialClass();

try

{

ObjectOutputStream out;

out= new ObjectOutputStream(new FileOutputStream(filename));

out.writeObject(time); //Write byte stream to file system.

out.close();

}

catch(IOException ex)

{

ex.printStackTrace();

}

}

}

How to recreate an object from its serialized state

The stored object can be read from file system at later time using java.io.ObjectInputStream as shown below:

import java.io.*;

public class ReadSerialClass

{

public static void main(String [] args)

{

String filename = "time.ser";

SerialClass time = null;

try

{

ObjectInputStream in;

in= new ObjectInputStream(new FileInputStream(filename));

time = (SerialClass)in.readObject();

in.close();

}

catch(Exception ex)

{

ex.printStackTrace();

}

// print out restored time

System.out.println ("Restored time: " + time.getTime());

}

}

Note: Serializing an object serializes the entire object graph of which it is the root, and operates correctly in the presence of cyclic graphs. A reset() method is provided to force the ObjectOutputStream to forget about objects that have already been serialized.