【发布时间】:2010-05-28 06:25:53
【问题描述】:
所以我试图将一个对象写出到一个 ByteArray,但由于某种原因它没有写任何东西,我看到返回值为 0,并且读取它会导致例外。
BAoutput = new ByteArrayOutputStream();
Oout = new ObjectOutputStream(BAoutput);
Oout.writeObject(receiver);
receiver 是我通过参数获得的对象。
并且例外总是相同的:
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
有什么想法吗?
大部分代码:(上面有几个定义,没什么意思)
try {
BAoutput = new ByteArrayOutputStream();
Oout = new ObjectOutputStream(BAoutput);
BAinput = new ByteArrayInputStream(BAoutput.toByteArray());
Oin = new ObjectInputStream(BAinput);
Oout.writeObject(receiver);
retval = method.invoke(receiver, args);
for (Method curr: postMethods){
curr.setAccessible(true);
if (curr.invoke(receiver).equals(false)){
receiver = Oin.readObject();
throw new PostconditionFailure();
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
Oin.close();
Oout.close();
BAinput.close();
BAoutput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
【问题讨论】:
-
你刷新/关闭输出流吗?
-
实际抛出的异常是什么?
-
请添加异常的第一行(异常类型和消息)
标签: java serialization bytearray