如果我实现writeObject或readObject方法,如果我不调用defaultWriteObject()/ defaultReadObject()会发生什么? 是不是要编写类元数据(类名等),这通常是在默认序列化期间完成的?
例外:
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)
为什么java会抛出此异常?如果我在路径中有类,则不会抛出此异常。如果该类的元数据不存在,那么如何进行交叉检查才能看到该类在路径中?
这是我写的可序列化类:
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();
}
}
}