When your class implements Serializable interface, object serialization is automatic where you just need to call writeObject() and readObject() methods for serialization and deserialization. If you want to customize the serialization process with you having more control then you can use Externalizable interface in Java.
Externalizable interface in Java
java.io.Externalizable interface extends Serializable interface and adds two methods of its own-
- writeExternal(ObjectOutput out)- To write object into a stream by calling writexxx methods for primitive types and writeObject method for objects. This flexibility to call write() methods for individual fields gives you control over the serialization process.
- readExternal(ObjectInput in)- To read object from stream by calling readxxx methods for primitive types and readObject method for objects.
Any class where you want to have control over its object serialization should implement writeExternal and readExternal methods of the Externalizable interface.
Externalizable Java example
In the example we have an Employee class that implements Externalizable interface. There are four fields in the Employee class, in the writeExternal() method you can control which of these fields are to be written to the stream. In this example salary field is omitted.
import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; public class Employee implements Externalizable{ private String name; private String dept; private int salary; private int age; // no arg constructor required public Employee(){} Employee(String name, String dept, int salary, int age){ this.name = name; this.dept = dept; this.salary = salary; this.age = age; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDept() { return dept; } public void setDept(String dept) { this.dept = dept; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } @Override public void writeExternal(ObjectOutput out) throws IOException { System.out.println("In writeExternal method"); out.writeObject(name); out.writeObject(dept); out.writeInt(age); } @Override public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { System.out.println("In readExternal method"); name = (String)in.readObject(); dept = (String)in.readObject(); age = in.readInt(); } }
Here is a class that does the serialization and deserialization of an Employee class object.
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class ExternalizableDemo { public static void main(String[] args) { Employee emp = new Employee("Ryan", "IT", 7500, 34); final String fileName = "F:\\knpcode\\emp.ser"; ObjectOutputStream outStream; try { // Serializing object outStream = new ObjectOutputStream(new FileOutputStream(fileName)); outStream.writeObject(emp); outStream.close(); // Deserializing object ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(fileName)); emp = (Employee)inputStream.readObject(); inputStream.close(); System.out.println("Name: " + emp.getName() + " Dept: " + emp.getDept() + " Salary: " + emp.getSalary() + " Age: " + emp.getAge()); } catch (IOException | ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }Output
In writeExternal method In readExternal method Name: Ryan Dept: IT Salary: 0 Age: 34
Some important points about Externalizable-
- When an Externalizable object is reconstructed during the deserialization process, object instance is created first using the public no-argument constructor, then the readExternal method is called. So, ensure that the class implementing Externalizable has a public no-arg constructor.
- In readExternal() method fields should be read in the same order they were written in the writeExternal() method otherwise an exception is thrown.
Serialization order in Java
Externalizable takes precedence over Serialization. Each object that is serialized is tested for the Externalizable interface. If the object supports Externalizable, the writeExternal method is called. If the object does not support Externalizable and does implement Serializable, the object is saved using ObjectOutputStream.
When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor, then the readExternal method called. Serializable objects are restored by reading them from an ObjectInputStream.
That's all for the topic Java Externalizable Interface Example. If something is missing or you have something to share about the topic please write a comment.
You may also like
- Serialization Proxy Pattern -readResolve() and writeReplace()
- Phaser in Java With Examples
- LocalDateTime in Java With Examples
- Chained Exception in Java
- Generating PDF in Java Using PDFBox Tutorial
- Hadoop MapReduce Word Count Program
- Spring Component Scanning Example
- spring-boot-starter-parent in Spring Boot
No comments:
Post a Comment