There
might be times when you have special requirements for the serialization of an
object. For example, you may have some security-sensitive parts of the object,
like passwords, which you do not want to keep and transfer somewhere. Or, it
may be worthless to save a particular object referenced from the main object
because its value will become worthless after restoring.
Unlike
Serializable interface, Externalizable interface is NOT a marker interface
One thing you can do
with Externalization is that you can store extra information into object like
STATIC variables and transient variables or you can add more information if you
have any business need. One good example is compressing and uncompressing of
data to send it through network or converting one format to other like a BMP
image to JPEG or GIF format.
You can control the process of serialization by implementing the
Externalizable interface instead of Serializable. This interface extends the
original Serializable interface and adds writeExternal() and readExternal(). which
are to be overridden for the purpose of object serialization process.
writeExternal() method is used to save the contents of an object.
readExternal() method is used to restore the contents of an object.
·
These two methods will automatically be called in your object's
serialization and deserialization, allowing you to control the whole process.
·
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.
There is one major difference between serialization and
externalization:
When you serialize an Externalizable object, a default
constructor will be called automatically; only after that will the
readExternal() method be called.
If you inherit some class from a class implementing the
Externalizable interface, you must call writeExternal() and readExternal()
methods when you serialize or deserialize this class in order to correctly save
and restore the object.
How
serialization happens?
- 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, then Serializable objects are restored by reading them from an ObjectInputStream.
Externalization Code
public class FirstClass implements Externalizable
{
SecondClass secondClass;
public void writeExternal(ObjectOutputStream out)
{
secondClass.writeExternal(out);
}
public void readExternal(ObjectInputStream inp)
{
secondClass = new SecondClass();
secondClass.readExternal(inp);
}
}
class SecondClass implements Externalizable
{
String str;
public void writeExternal(ObjectOutputStream out) { out.writeUTF(str); }
public void readExternal(ObjectInputStream inp) { str = inp.readUTF(); }
}
{
SecondClass secondClass;
public void writeExternal(ObjectOutputStream out)
{
secondClass.writeExternal(out);
}
public void readExternal(ObjectInputStream inp)
{
secondClass = new SecondClass();
secondClass.readExternal(inp);
}
}
class SecondClass implements Externalizable
{
String str;
public void writeExternal(ObjectOutputStream out) { out.writeUTF(str); }
public void readExternal(ObjectInputStream inp) { str = inp.readUTF(); }
}
No comments:
Post a Comment