Externalizable in Java

We can customise the Serialization process by the following two ways:

  1. Implementing Externalizable interface.
  2. Implement Serializable and add the following methods to customise the serialization process.
    • private Object writeReplace() throws ObjectStreamException
    • private void writeObject(ObjectOutputStream out) throws IOException
    • private void readObject(ObjectInputStream in) throws IOException
    • private Object readResolve() throws ObjectStreamException
    • public void validateObject() throws InvalidObjectException

The methods writeReplace and readResolve can also be used along with Externalization.
please visit Magic methods to Customize Serialization for more details.

In this blog we will look into Externalization.

Externalization

Here , we have complete control over the serialization process.We can choose the fields that we wish to serialize and ignore other fields.Here our class must implement Externalizable interface and then override readExternal and writeExternal methods.

In the below example, we are only serializing the field “name”.

package com.serialize.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;
public class ExternalizableExample implements Externalizable{
private String name;
private Integer version;
private static final String fileName =”externalizationExample.ser”;
public ExternalizableExample(String name, Integer version) {
this.name = name;
this.version = version;
}
public ExternalizableExample() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getVersion() {
return version;
}

public void setVersion(Integer version) {
this.version = version;
}

@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(name);
}

@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
name=(String)in.readObject();
}

public static void main(String[] args) throws FileNotFoundException,
IOException, ClassNotFoundException {
ExternalizableExample externalizableExample = new ExternalizableExample(“Gyan”, 1);
System.out.println(“Before Externalization”);
System.out.println(“Name:” + externalizableExample.getName());
System.out.println(“version:” + externalizableExample.getVersion());
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(fileName));
out.writeObject(externalizableExample);
out.close();
System.out.println(“After Externalization”);
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
ExternalizableExample externalizableExample1 = (ExternalizableExample) in.readObject();
in.close();
System.out.println(“Name:” + externalizableExample1.getName());
System.out.println(“version:” + externalizableExample1.getVersion());
}

}

Output:
Before Externalization
Name:Gyan
version:1
After Externalization
Name:Gyan
version:null

Leave a Reply

Your email address will not be published. Required fields are marked *