我正在尝试使用套接字通过 ObjectOutputStream 加载对象。
我在同一台机器上测试。
我收到此错误:
java.io.StreamCorruptedException: invalid type code: 00
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.readArray(Unknown Source)
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)
我使用此代码创建 ObjectOutputStream:
InputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream objectInputStream = new ObjectInputStream(in);
Object readObject = objectInputStream.readObject();
更奇怪的是,当我通过不是那么大的物体时没有问题。
我读取的返回问题的对象的字节大小约为:73423 字节。
我通过 ObjectOutputStream 传递了一个 java.util.Vector。
如果我传递的数量大于 5,则会引发上述异常。
我还创建了一个简单的测试(不通过套接字)以确保对象是可序列化的,它可以正常工作。NNTraining 是我要序列化的类:
Vector<NNTraining> nn = new Vector<NNTraining>();
ByteArrayOutputStream b = new ByteArrayOutputStream();
ObjectOutputStream o = new ObjectOutputStream(b);
o.writeObject(nn);
o.close();
ByteArrayInputStream bb = new ByteArrayInputStream(b.toByteArray());
ObjectInputStream oo = new ObjectInputStream(bb);
Vector<NNTraining> v2 = (Vector<NNTraining>) oo.readObject();