Externalization

  • Externalization is nothing but serialization but by implementing Externalizable interface to persist and restore the object.
  • To externalize your object, you need to implement Externalizable interface that extends Serializable interface
  • Here only the identity of the class is written in the serialization stream and it is the responsibility of the class to save and restore the contents of its instances which means you will have complete control of what to serialize and what not to serialize.
  • Unlike Serializable interface, Externalizable interface is not a marker interface and it provides two methods - writeExternal and readExternal. These methods are implemented by the class to give the class a complete control over the format and contents of the stream for an object and its supertypes.
  • These methods must explicitly coordinate with the supertype to save its state. These methods supersede customized implementations of writeObject and readObject methods.

Let's see an example :

Example

` import java.io.*;
public class Car implements Externalizable
{
String name;
int year;
/*
* mandatory public no-arg constructor
*/
public Car()
{
super();
}
Car(String n, int y)
{
name = n;
year = y;
}
/** * Mandatory writeExernal method. */
public void writeExternal(ObjectOutput out) throws IOException
{
out.writeObject(name);
out.writeInt(year);
}
/*** Mandatory readExternal method.*/
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException
{
name = (String) in.readObject();
year = in.readInt();
}
/** * Prints out the fields. used for testing!*/
public String toString()
{
return("Name: " + name + "\n" + "Year: " + year);
}
}

Example

import java.io.*;
public class ExternExample
{
public static void main(String args[])
{
// create a Car object
Car car = new Car("Mitsubishi", 2009);
Car newCar = null;
//serialize the car
try
{
FileOutputStream fo = new FileOutputStream("tmp");
ObjectOutputStream so = new ObjectOutputStream(fo);
so.writeObject(car);
so.flush();
}
catch (Exception e)
{
System.out.println(e);
System.exit(1);
}
// de-serialize the Car
try
{
FileInputStream fi = new FileInputStream("tmp");
ObjectInputStream si = new ObjectInputStream(fi);
newCar = (Car) si.readObject();
}
catch (Exception e)
{
System.out.println(e);
System.exit(1);
}
/* * Print out the original and new car information*/
System.out.println("The original car is ");
System.out.println(car);
System.out.println("The new car is ");
System.out.println(newCar);
}
}

In this example, class Car implements Externalizable interface which means that car object is ready for serialization. This class have two public methods - "writeExternal" and "readExternal". Unlike Serializable interface which will serialize all the variables in the object with just by implementing the interface, here you have to explicitly mention what fields or variables you want to serialize and the same is done in "writeExternal" and "readExternal" methods. So in the "ExternExample" class, when you write the "Car" object to the OutputStream, the "writeExternal" method is called and the data is persisted. The same applies to "readExternal" method in the Car object i.e., when you read the "Car" object from the ObjectInputStream, "readExternal" method is called.

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment