Friday, 19 October 2012

Externalizable interface example for Serialization and Deserialization


Difference between Externalization and Serialization

Externalizable gives control over the read/write process by implementing the readExternal and writeExternal methods. In Serializable JVM has full control for serializing object but on the other hand in Externalizable application/developer gets control for persisting objects. writeExternal() and readExternal() method provides complete control on format and content of Serialization process.

1. In Externalization custom Serialization process is used which is implemented by application but in Serialization, default serialization process is used.

2. JVM gives call back to readExternel() and writeExternal() of java.io.Externalizalbe interface for restoring and writing objects into persistence.
 



//This is the sample program for using Externalizable interface for Serialization and Deserialization

package com.corejava.gaurav.examples;

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;

class First implements Externalizable {
    public First() {
        System.out.println("Constructor of class First");
    }

    public void writeExternal(ObjectOutput out) {
        System.out.println("WriteExternal method of class First");
    }

    public void readExternal(ObjectInput in) {
        System.out.println("ReadExternal method of class First");
    }
}

class Second implements Externalizable {
    public Second() {
        System.out.println("Constructor of class Second");
    }

    public void writeExternal(ObjectOutput out) throws IOException {
        System.out.println("WriteExternal method of class Second");
    }

    public void readExternal(ObjectInput in) throws IOException,
            ClassNotFoundException {
        System.out.println("ReadExternal method of class Second");
    }
}

public class ExternalizableImplementation {
    public static void main(String[] args) throws IOException,
            ClassNotFoundException {
        System.out.println("Constructing objects to persist:");
        First objectDemo1 = new First();
        Second objectDemo2 = new Second();
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream(
                "ExternalizationExampleFile.out"));
        System.out.println("Persisting objects:");
        o.writeObject(objectDemo1);
        o.writeObject(objectDemo2);
        o.close();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                "ExternalizationExampleFile.out"));
        System.out.println("Reading back the objectDemo1:*****************");
        objectDemo1 = (First) in.readObject();
        System.out.println("Reading back the objectDemo2:*****************");
        objectDemo2 = (Second) in.readObject();

    }
}

No comments:

Post a Comment