Externalization is an alternative way for implementing the serialization mechanism.Instead of implementing the Serializable interface, you can implement Externalizable, which contains two methods:
public interface Externalizable
extends Serializable
writeExternal
public void writeExternal(ObjectOutput out)
throws IOException
The object implements the writeExternal method to save its contents by calling the methods of DataOutput for its primitive values or calling the writeObject method of ObjectOutput for objects, strings, and arrays.
readExternal
public void readExternal(ObjectInput in)
throws IOException,
ClassNotFoundException
The object implements the readExternal method to restore its contents by calling the methods of DataInput for primitive types and readObject for objects, strings and arrays. The readExternal method must read the values in the same sequence and with the same types as were written by writeExternal.
How serialization works?
JVM first checks for the Externalizable interface and if object supports Externalizable interface, then serializes the object using writeExternal method. If the object does not support Externalizable but implement Serializable, then the object is saved using ObjectOutputStream. Now when an Externalizable object is reconstructed, an instance is created first using the public no-arg constructor, then the readExternal method is called. Again if the object does not support Externalizable, the Serializable objects are restored by reading them from an ObjectInputStream.
Lets see an example.
package extern; import java.io.Externalizable; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; public class ExternalizationExample { /** * @param args * @throws ClassNotFoundException **/ public static void main(String[] args) throws ClassNotFoundException { ObjectOutputStream out = null; ObjectInputStream in = null; try { out = new ObjectOutputStream(new FileOutputStream("employee.ser")); /** Create one Employee object to serialize **/ Employee emp = new Employee("John", 12345, "Banking"); /** Serialize the object emp and write to file employee.ser **/ out.writeObject(emp); in = new ObjectInputStream(new FileInputStream("employee.ser")); /** Deserialize the object stored in the file employee.ver **/ Employee emp_copy = (Employee) in.readObject(); System.out.println(emp_copy); } catch (IOException e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } } class Employee implements Externalizable { String empName; int empno; String dept; public Employee(String empName, int empno, String dept) { this.empName = empName; this.empno = empno; this.dept = dept; } /** * mandatory public non-arg constructor. It will be invoked during * deserialization process. **/ public Employee() { } /** Override the toString method to print object fields **/ public String toString() { return ("Employee Name =" + empName + " & Employee No =" + empno + " &Department =" + dept); } /** manadatory method.overridden to restore the object state from stream **/ @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { empName = in.readUTF(); empno = in.readInt(); dept = in.readUTF(); } /** manadatory method. overridden to save the object state to stream **/ @Override public void writeExternal(ObjectOutput out) throws IOException { out.writeUTF(empName); out.writeInt(empno); out.writeUTF(dept); } }
The output will be
Employee Name =John & Employee No =12345 &Department =Banking
In the above example, Employee class implements Externalizable interface and overrides the readExternal and writeExternal methods of Externalizable interface.The writeExternal method is overridden to save the selected field values in the stream.The readExternal method is overridden to restore the field values from the stream.
The writeExternal method will be invoked by the system when you call writeObject from the ExternalizationExample.The non-arg constructor and then readExternal method will be invoked by the system when you call the readObject.
No comments:
Post a Comment