Revision: 64540
Updated Code
at August 20, 2013 10:17 by rnavanee
Updated Code
package com.test.jvm;
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 ExternalizableTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyTestObject testObj = new MyTestObject();
testObj.setL(12345678L);
testObj.setS("hello");
// Serialize object
FileOutputStream fos = new FileOutputStream("obj.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(testObj);
// Deserialize object
FileInputStream fis = new FileInputStream("obj.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
MyTestObject objRead = (MyTestObject)ois.readObject();
System.out.println("objRead: s="+objRead.getS()+", l="+objRead.getL());
}
}
class MyTestObject implements Externalizable {
private String s;
private long l;
public MyTestObject() {
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public long getL() {
return l;
}
public void setL(long l) {
this.l = l;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.s);
out.writeLong(this.l);
System.out.println("writeExternal complete");
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
this.s = (String) in.readObject();
this.l = in.readLong();
System.out.println("readExternal complete");
}
}
/*
#1:
Output:
writeExternal complete
readExternal complete
objRead: s=hello, l=12345678
#2:
If the no-arg constructor of MyTestObject is commented out, you'll get the below error:
Exception in thread "main" java.io.InvalidClassException: com.test.jvm.MyTestObject; com.test.jvm.MyTestObject; no valid constructor
at java.io.ObjectStreamClass.checkDeserialize(ObjectStreamClass.java:724)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1744)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1340)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:362)
at com.test.jvm.ExternalizableTest.main(ExternalizableTest.java:27)
Caused by: java.io.InvalidClassException: com.test.jvm.MyTestObject; no valid constructor
at java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:482)
at java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:321)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1117)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:337)
at com.test.jvm.ExternalizableTest.main(ExternalizableTest.java:22)
*/
Revision: 64539
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at August 20, 2013 10:13 by rnavanee
Initial Code
package com.test.jvm;
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 ExternalizableTest {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyTestObject testObj = new MyTestObject();
testObj.setL(12345678L);
testObj.setS("hello");
// Serialize object
FileOutputStream fos = new FileOutputStream("obj.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(testObj);
// Deserialize object
FileInputStream fis = new FileInputStream("obj.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
MyTestObject objRead = (MyTestObject)ois.readObject();
System.out.println("objRead: s="+objRead.getS()+", l="+objRead.getL());
}
}
class MyTestObject implements Externalizable {
private String s;
private long l;
public MyTestObject() {
}
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public long getL() {
return l;
}
public void setL(long l) {
this.l = l;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(this.s);
out.writeLong(this.l);
System.out.println("writeExternal complete");
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
this.s = (String) in.readObject();
this.l = in.readLong();
System.out.println("readExternal complete");
}
}
Initial URL
http://www.dzone.com/links/r/optimizing_java_serialization_java_vs_xml_vs_json.html
Initial Description
Externalizable interface provides you an alternate to Serializable interface. In Externalizable, you specify how/what to persist. Seralizable persists all non-transient elements using default serialize/deserialize implementation.
Initial Title
Externalizable Example
Initial Tags
java
Initial Language
Java