如何解决Java XStream - EOFException createObjectInputStream
------------------------------------------------------------------------
java.io.EOFException
------------------------------------------------------------------------
java.base/java.io.DataInputStream.readFully(DataInputStream.java:201)
java.base/java.io.DataInputStream.readUTF(DataInputStream.java:613)
java.base/java.io.DataInputStream.readUTF(DataInputStream.java:568)
com.thoughtworks.xstream.io.binary.Token.readString(Token.java:153)
com.thoughtworks.xstream.io.binary.Token$MapIdTovalue.readFrom(Token.java:227)
com.thoughtworks.xstream.io.binary.Token$Formatter.read(Token.java:186)
com.thoughtworks.xstream.io.binary.BinaryStreamReader.readToken(BinaryStreamReader.java:153)
com.thoughtworks.xstream.io.binary.BinaryStreamReader.moveDown(BinaryStreamReader.java:82)
com.thoughtworks.xstream.io.binary.BinaryStreamReader.<init>(BinaryStreamReader.java:45)
com.thoughtworks.xstream.io.binary.BinaryStreamDriver.createReader(BinaryStreamDriver.java:43)
我不知道为什么。仅对某些对象发生。不是全部。
发生这种情况时,我正在尝试使用 BinaryStreamDriver。正常的 toXML 和 fromXML 操作工作正常。
使用的代码:
XStream XSTREAM = new XStream(new BinaryStreamDriver()) {
// https://x-stream.github.io/faq.html
{
this.ignoreUnkNownElements();
}
};
这两个版本都会产生类似的错误:
private Object readA(byte[] bytes) {
try (
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInputStream xdis = STREAM.createObjectInputStream(bis);
) {
Object o = xdis.readobject();
xdis.close();
return o;
} catch (IOException | ClassNotFoundException e) {
throw Ex.runtime(e);
}
}
private byte[] writeA(Object object) {
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream xdos = STREAM.createObjectOutputStream(bos);
) {
xdos.writeObject(object);
xdos.flush();
xdos.close();
byte[] bytes = bos.toByteArray();
return bytes;
}
catch (IOException e) {
throw Ex.runtime(e);
}
}
还有这个:
private Object readB(byte[] bytes) {
try (
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
BufferedInputStream dis = new BufferedInputStream(bis);
) {
Object object = STREAM.unmarshal(new BinaryStreamReader(dis));
return object;
} catch (IOException e) {
throw Ex.runtime(e);
}
}
private byte[] writeB(Object object) {
try (
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bufferedoutputstream dos = new bufferedoutputstream(bos);
) {
BinaryStreamWriter writer = new BinaryStreamWriter(dos);
STREAM.marshal(object,writer);
writer.flush();
writer.close();
byte[] bytes = bos.toByteArray();
return bytes;
}
catch (IOException e) {
throw Ex.runtime(e);
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。