Externalizable Interface in Java

Serialization is the process of giving persistence storage to Java objects so that they can be restored at a later time. Classes can be made persistent in Java by implementing the Serializable interface. Serializable is a marker interface meaning that it has no methods within it. Any Serializable class can be passed on to ObjectOutputStream.writeObject(object) for saving the object state and a call back to ObjectInputStream.readObject() will get the object back from the storage.

also read:

Since this is a default Serialization process, the Application cannot have a finer control over the Serialization process. Because, by default all the instance variables, except static and transient variables will undergo Serialization process.

Let us see in this article how to take full control over the Serialization process by depending on the Externalizable interface. This interface has two methods called writeExternal() and readExternal() that needs to be overridden for making up the Serialization process.
Let us say that we wish to make the following Movie class to implement the Externalizable interface for custom Serialization. Note that in writeExternal() method, we store all the fields to the Output Stream by calling the ObjectOutput.write###() methods. And during the restoration process, readExternal() will be called, where the implementation stores the appropriate values in their corresponding fields.
Movie.java

package tips.externaizable;
    public class Movie implements Externalizable{
        private String name;
        private long releaseYear;
        private String language;
        public Movie(){
        }
        public Movie(String name, long releaseYear, String language){
            this.name = name;
            this.releaseYear = releaseYear;
            this.language = language;
        }
        public String toString(){
            return name + " , " + releaseYear + " , " + language;
        }
        @Override
        public void readExternal(ObjectInput in)
        throws IOException,ClassNotFoundException {
            name = in.readUTF();
            releaseYear = in.readLong();
            language = in.readUTF();
        }
        @Override
        public void writeExternal(ObjectOutput out)
        throws IOException {
            out.writeUTF(name);
            out.writeLong(releaseYear);
            out.writeUTF(language);
        }
    }

Following is the Client program that makes use of the above Movie class. The Object Output Stream class comes into picture for saving the object state in a file called "movies". For retrieving the object back from the storage we use the Object Input Stream class.
MovieExternalizableTest.java

  package tips.externalizable;
    import java.io.*;
    public class MovieExternalizableTest {
        public static void main(String[] args) throws Exception{
            Movie harry = new Movie(
                "Harry Potter and the Order of Phoenix", 2007, "English");
            ObjectOutputStream output = new ObjectOutputStream(
                new FileOutputStream("movies"));
            output.writeObject(harry);
            output.close();
            ObjectInputStream input = new ObjectInputStream(
                new FileInputStream("movies"));
            harry = (Movie)input.readObject();
            System.out.println(harry);
            input.close();
        }
    }

Comments

comments

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Comments

  1. mahesh patidar says:

    Hey many many thanks for nice explanation.

Speak Your Mind

*