Sunday, December 20, 2009

Externalizable

java.io.Externalizable



Externalizable interface extends java.io.Serializable, whereas this interface comes with two methods to store and retrieve data from Object Stream and this is not marker interface. This approach gives luxury of deciding to application developer, what to be serilized and what not to be. However, this technique enforces that the order in it stored in stream, the same order, has to be used to read back.




import java.io.Externalizable;
import java.io.File;
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 ExternalizableTest {

public static class EmployeeBean implements Externalizable {

private static final long serialVersionUID = 4258520358186173223L;

String name;

String designation;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getDesignation() {
return designation;
}

public void setDesignation(String designation) {
this.designation = designation;
}

public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
// TODO Auto-generated method stub

name= (String)in.readObject();
designation=(String)in.readObject();

}

public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
out.writeObject(name);
out.writeObject(designation);
}
}
public static void main(String[] args) throws Exception {

ExternalizableTest.EmployeeBean emp = new EmployeeBean();

emp.setName("Krishna");

FileOutputStream bos = new FileOutputStream(new File("/tmp/a.txt"));
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(emp);
out.close();

FileInputStream bis = new FileInputStream(new File("/tmp/a.txt"));
ObjectInputStream in = new ObjectInputStream(bis);
EmployeeBean empFromPersist = (EmployeeBean) in.readObject();
System.out.println("Employee name from persisted Bean ::" + empFromPersist.getName());
in.close();
}

}



The above code will stores and retrieves two fields.



If number of read operations is not equivalent in readExternal to write operations in writeExternal then following Exception



Exception in thread "main" java.io.OptionalDataException
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1349)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at ExternalizableTest$EmployeeBean.readExternal(ExternalizableTest.java:42)
at java.io.ObjectInputStream.readExternalData(ObjectInputStream.java:1792)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1751)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)
at ExternalizableTest.main(ExternalizableTest.java:65)

Reactions:

0 comments:

Post a Comment

Recent Posts

Unix Commands | List all My Posts

Texts

This blog intended to share the knowledge and contribute to JAVA Community such a way that by providing samples and pointing right documents/webpages. We try to give our knowledege level best and no guarantee can be claimed on truth. Copyright and Terms of Policy refer blogspot.com

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More