Wednesday, 8 February 2017

EXTERNALIZATION IN JAVA

In Serialization entire ability will be provided by jvm that is jvm is responsible for both marshalling and unmarshalling of an object. Programmers doesn’t have any control on serialization. In serialization total state of the object will be saved compulsorily and it isn’t possible to save part of the object state. It increases complexity and creates performance problems.

To overcome these problems we should go for Externalization, where complete control is in the programmers hand (i.e., our hand). Based on the programming requirement we can save either total state of the object or part of the object so that performance will be improved relatively.

To provide Externalizable ability for any object compulsorily the corresponding class should implement from Externalizable interface. This interface is available in java.io package which is the child interface of Serializable and contains two methods –
1. writeExternal(ObjectOutput os)throws Exception – This method will be executed automatically at the time of serialization of an object. While performing serialization variable values which are required to save the corresponding code have to be written inside this method.
2. readExternal(ObjectInput oi) throws Exception – This method will be executed automatically at the time of Deserialization of an object. In this method we have to write the code for reading data from the file and assigning to the current object instance variable. At the time of Deserialization, jvm will create a separate object by executing public no argument constructor, class should compulsorily contain public no-argument constructor. Every Externalizable class should compulsorily contain public no-argument constructor, otherwise we will get RuntimeException

Example –
import java.io.Externalizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

class Example2 implements Externalizable{
          String s;
          int i;
          int j;
          public Example2()
          {
                   System.out.println("This constructor will be automatically invoked during deserialization");
          }
          public Example2(String s,int i,int j)
          {
                   this.s=s;
                   this.i=i;
                   this.j=j;
          }
          @Override
          public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
                   s=(String)in.readObject();
                   i=in.readInt();
          }
          @Override
          public void writeExternal(ObjectOutput out) throws IOException {
                   out.writeObject(s);
                   out.writeInt(i);
                   out.writeInt(j);            
          }       
}
public class Externalizables {
          public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
                   Example2 obj1 = new Example2("rakesh",10,20);
                   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("lmn.txt"));
                   oos.writeObject(obj1);
                   ObjectInputStream ois = new ObjectInputStream(new FileInputStream("lmn.txt"));
                   Example2 obj2 = (Example2)ois.readObject();
                   System.out.println(obj2.s);
                   System.out.println(obj2.i);
                   System.out.println(obj2.j);
          }
}

Output –
This constructor will be automatically invoked during deserialization
rakesh
10
0 

No comments:

Post a Comment