Serialization Mechanism
Serialization is the mechanism which is used to represent the object as sequence of bytes that includes object's data, type of object
and type of data stored in the object. Serialized object can be transferred over the network and serialized object can be stored in system as a file.
Serialized objects can be read from the file and de-serialized that which means serialized objects can be loaded into JVM through de-serialization process.
ObjectOutputStream and ObjectInputStream classes are the high-level stream classes that contains the methods for serialization and de-serialization.
ObjectOutputStream class contains write method for serialization as below
public final void writeObject(Object x) throws IOException
-->This method will serialize the object and it can be transferred over the network or it can be stored as a file.
ObjectInputStream class contains read method for de-serialization as below
public final Object readObject() throws IOException,
ClassNotFoundException
-->This method will de-serialize the object and it returns the value as Object, the we have cast it to appropriate object type.
For successful serialization below two condition must be met,
- The class must implement the java.io.Serializable interface.
- All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.
public class Employee implements java.io.Serializable {
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}
In above example, Employee class is implementing java.io.Serializable interface, so we can able to serialize the Employee object, but in Employee class, SSN field is declared as transient, so SSN field value can't be serialized.
Serializing an Object
ObjectOutputStream class is being used to serializing an object. We will see in below example that, how Employee object can be serialized
import java.io.*;
public class SerializeObject {
public static void main(String [] args) {
Employee e = new Employee();
e.name = "Kannan";
e.address = "Chennai";
e.SSN = 12345;
e.number = 333;
try {
FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
} catch(IOException i) {
i.printStackTrace();
}
}
}
When executing an above program Employee object will be serialized and stored as file in given directory.
Note: When serializing an object to a file, the standard convention in Java is to give the file a .ser extension.
De-Serializing an Object
We will de-serialize the Employee object which we have serialized already in above example.
ObjectInputStream will be used to de-serialize on object
import java.io.*;
public class DeserializeObject {
public static void main(String [] args) {
Employee e = null;
try {
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
} catch(IOException i) {
i.printStackTrace();
return;
} catch(ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
When we are executing above program, it will give the below result,
Deserialized Employee...
Name: Kannan
Address:Chennai
SSN: 0
Number:333
Here are following important points to be noted:
- The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for the class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.
- Notice that the return value of readObject() is cast to an Employee reference.
- The value of the SSN field was 11122333 when the object was serialized, but because the field is transient, this value was not sent to the output stream. The SSN field of the deserialized Employee object is 0.
No comments:
Post a Comment