Externalizable interface in Java
Externalization is a technique that allows the user to customize the serialization process and also allows them to specify the data to serialize and how to serialize it, unlike default serialization which automatically serializes all fields.
The Externalizable interface, which is part of the java.io package in Java, extends the Serializable interface, indicating that classes that implement Externalizable can be serialized. It is a customized version of the Serializable interface that gives developers complete control over serialization and deserialization. Externalizable provides explicit methods for reading objects from stream and writing objects to a stream.
Methods:
When a class implements the Externalizable interface, it must override the two methods. They are:
- readExternal() method
- writeExternal() method
1) readExternal() method: The readExternal() method is called when an object is deserialised. It reads the serialized data from the stream in the same order and format as it was written by the writeExternal() method. It maintains the object original state using ObjectInput methods, specifically the readObject() methods for objects, strings, and arrays. It uses primitive data type readers like readInt() and readFloat() for basic data types and readObject() for complex data like objects, strings, and arrays.
2) writeExternal() method: The writeExternal() method is used when an object is serialized and handles its data output. It uses ObjectOutput methods to write the object data into a serialized stream, ensuring that complex data types like objects, strings, and arrays are correctly stored and maintained.
void readExternal() | void writeExternal() |
Also called ObjectInput inStream. | Also called ObjectOutput outStream. |
The readExternal() method returns the data of the caller object by calling various methods on the specified ObjectInput ipStream. In other words, we use the readExternal() method to read data from a stream and store them in an object. | The writeExternal() method writes the data of the caller object by calling various methods on the specified ObjectOutput outStream. In other words, the writeExternal() method is used to write fields of an object to a stream. |
Syntax: public void readExternal(ObjectInput inStream) throws IOException, ClassNotFoundException { //write the logic here for reading object fields from the stream. } | Syntax: public void writeExternal(ObjectOutput outStream) throws IOException { // write the logic here to write object fields to a stream. } |
Purpose:
The Externalizable interface is used for custom serialization. Implementing this interface allows users to specify exactly how the object state is stored and returned, providing users more control over the serialization process than the default serialiazation process provided by the Serializable interface.
Example:
The example shows how to use the Externalizable interface in Java for custom serialization and deserialization of an object. We create a class named Student, assign its properties, and then write it to a file using ObjectOutputStream. Then, we retrieve the object from the file using ObjectInputStream and display its properties.
FileName: ExternalizableInterfacesExample.java
// importing required io classes and interfaces 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 ExternalizableInterfacesExample { // main method public static void main(String[] args) { // Creating an object for Student class Student sdnt = new Student(); sdnt.studentID = "id22509"; sdnt.studentName = "Harsha Vardhan"; sdnt.studentAge = 20; // writing object sdnt to the file named test.txt try ( FileOutputStream fOptStrm = new FileOutputStream("test.txt"); ObjectOutputStream objOptStrm = new ObjectOutputStream(fOptStrm); ) { objOptStrm.writeObject(sdnt); // serializing the object to the file } catch (IOException ie) { System.out.println(ie.getMessage()); } // reading object from the file named test.txt try ( FileInputStream fIptStrm = new FileInputStream("test.txt"); ObjectInputStream objIptStrm = new ObjectInputStream(fIptStrm); ) { sdnt = (Student) objIptStrm.readObject(); // deserializing the object from the file // printing the deserialized object properties System.out.println("Student ID: " + sdnt.studentID); System.out.println("Student Name: " + sdnt.studentName); System.out.println("Student Age: " + sdnt.studentAge); } // handling io exceptions catch (IOException ie) { System.out.println(ie.getMessage()); } // handling class not found exception catch (ClassNotFoundException ce) { System.out.println(ce.getMessage()); } } } // Student class implementing Externalizable interface class Student implements Externalizable { public Student() {} // default constructor // class properties String studentID; String studentName; int studentAge; // implementing writeExternal method for serialization public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(studentID); out.writeObject(studentName); out.writeInt(studentAge); } // implementing readExternal method for deserialization public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { studentID = (String) in.readObject(); studentName = (String) in.readObject(); studentAge = in.readInt(); } }
Output:
Student ID: id22509 Student Name: Harsha Vardhan Student Age: 20.