Friday 24 February 2012

Externalization in java



Limitations of Serialization
1)File size is very high
2)Customization due to transient which  is not effective because we get “null” in place of transient attributes.
3)while customizing we also get a metainformation of the file which includes when created who are eligible to read it etc: which is against data security.


Inorder to address these limitations of serialization,sun people came up with another I/O process named Externalization,which refers to dumping the state of an object in a permanent media using the interface Externalizable.

Externalizable is a sub-interface to Serializable but it is not a marker interface because it has two unimplemented methods readExternal() and writeExternal() which should be implemented by the classes which use Externalizable interface.

The process of externalization is same as that of serialization except the following:
1)Implementing writeExternal(ObjectOutput oout) & readExternal(ObjectInput oin) methods of Externalizable interface in a class which we want to externalize
2)Employing writeExternal() and readExternal() in place writeObject() & readObject() respectively.In block of writeExternal(),keep the attributes which we like to externalize
3)customization is very easy incase of externalization because whatever attributes we want to keep away from externalization just don’t keep them inside writeExternal() method block
4) In writeExternal() method block we make attributes externalized by using corresponding methods for different types of data. We should take care of IOException while using this method.

   Ex:writeInt(i)----------àfor integers
      writeDouble(d)------àfor doubles
      writeUTF(s)-------àfor strings
      writeObject(i)-------àfor derived attributes other than string& wrapper         
                              classes
UTF--àUniversal Text Format

5) using readExternal() method we can read the states of the object returned using the corresponding read methods stated above
6)flusing and closing operations are same to that of serialization
7)All the rules of derived attributes & Inheritance applied for serialization are also valid in case of Externalization.
8)If a class uses any derived attributes other than String & all Wrapper class attributes then that particular class also should be implemented with either Externalizable or Serializable interface.If we don’t do so, we get NotSerializableException

The main advantages of externalization over serialization are:
1)File size is highly reduced(nearly 1/3)
2) customization is very easy and more effective.


Java program to demonstrate Externalization
package com.usr.io;

import java.io.Externalizable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

class Employee implements Externalizable {
       String name;
       double salary;
       int age;
       long cardNo;

       public void writeExternal(ObjectOutput out) throws IOException {
              out.writeUTF(name);
              out.writeInt(age);
              out.writeDouble(salary);

       }

       public void readExternal(ObjectInput in) throws IOException,
                     ClassNotFoundException {
              name = in.readUTF();
              age = in.readInt();
              salary = in.readDouble();

       }
}
while writing whatever order we follow the same order must be followed in reading also.

public class ExternalizationDemo {
       public static void main(String[] args) {
              Employee emp = new Employee();
              emp.name = "Alex";
              emp.age = 26;
              emp.salary = 34567.8;
              emp.cardNo = 65754534;
              File file = new File("employee.ser");
              FileOutputStream fos = null;
              ObjectOutputStream oos = null;
              try {
                     fos = new FileOutputStream(file);
                     oos = new ObjectOutputStream(fos);
                     emp.writeExternal(oos);
                     System.out.println("object persisted");
              } catch (IOException ex) {
                     ex.printStackTrace();
              } finally {
                     try {
                           if (oos != null) {
                                  oos.flush();
                                  oos.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
                     try {
                           if (fos != null) {
                                  fos.flush();
                                  fos.close();
                           }
                     } catch (IOException ex) {
                           ex.printStackTrace();
                     }
              }
       }
}