If I implement the writeObject or readObject method, what would happen if I do not call defaultWriteObject()/defaultReadObject()? Will the class meta data not be written (class name, etc.), which is usually done during default serialization?
- If the meta data of the class is not written, then would having the class of the serialized object in the path of the JVM be still required during deserialization? Here's what happened when I tried this out - kept the class in the path when serializaing it but took it out during deserialzation:
Exception:
Exception in thread "main" java.io.StreamCorruptedException: unexpected block data
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.fiberlink.sample.JwtCreator.main(JwtCreator.java:28)
Why would java throw this exception? If I have the class in the path, this exception is not thrown. If the meta data of the class isn't even there, how does it do a cross check to see of the class is in the path?
- If the meta data is still written, who does that? I have implemented the writeObject method in my serializable class and am not calling the default methods. Where would the meta data come from?
Here's the serializable class that I wrote:
public class TestSerial implements Serializable
{
private static final long serialVersionUID = 1L;
public transient int a = 4;
public String c = "ccccc";
public String b = "bbbbb";
private void writeObject(ObjectOutputStream os)
{
try {
os.writeInt(a); // 3
}
catch (Exception e) {
e.printStackTrace();
}
}
private void readObject(ObjectInputStream is)
{
try {
a = is.readInt();
System.out.println(a);
}
catch (Exception e) {
e.printStackTrace();
}
}
}