简体   繁体   English

java.io.StreamCorruptedException:意外的重置; 递归深度1

[英]java.io.StreamCorruptedException:unexpected reset; recursion depth 1

I'm trying to write to and read from one file with ObjectInputStream and ObjectOutputStream, in two threads. 我试图在两个线程中使用ObjectInputStream和ObjectOutputStream写入和读取一个文件。 Since I have to append to this file, so according to some online advise, I implement a class: 由于必须附加到此文件,因此根据一些在线建议,我实现了一个类:

public class AppendableObjectOutputStream extends ObjectOutputStream {

public AppendableObjectOutputStream(OutputStream out) throws IOException {
    super(out);
}

protected void writeStreamHeader() throws IOException {
    reset();
}
}

And the write logic is like: 而写逻辑就像:

synchronized (SDKLogger.failoverFile) {
        FileOutputStream fos = null;
        ObjectOutputStream oos = null;
        try {
            if (failoverFile.exists()) {
                fos = new FileOutputStream(SDKLogger.failoverFile, true);
                oos = new AppendableObjectOutputStream(fos);
            } else {
                fos = new FileOutputStream(SDKLogger.failoverFile, true);
                oos = new ObjectOutputStream(fos);
            }
            for (LogModel log : logs)
                oos.writeObject(log);

Because I have synchronized on the global file only used for this purpose, not referenced anywhere else, I don't understand why I meet this exception always. 因为我已经在仅用于此目的的全局文件上进行了同步,而在其他任何地方都未引用,所以我不明白为什么总是遇到这种异常。

java.io.StreamCorruptedException: unexpected reset; recursion depth: 1
at java.io.ObjectInputStream.handleReset(Unknown Source)
at java.io.ObjectInputStream.access$600(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.refill(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.skipBlockData(Unknown Source)
at java.io.ObjectInputStream.skipCustomData(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source) 

After searching the web, I find almost nothing on what this error is about and why one could come cross this error. 在网上搜索后,我几乎找不到关于该错误的原因以及为什么会遇到此错误的信息。

Thanks for your help in advance. 感谢您的帮助。

Calling reset() in writeStreamHeader() is completely pointless. writeStreamHeader()调用reset()是完全没有意义的。 You are still constructing the object stream at this point: there is nothing in it to reset yet. 此时,您仍在构建对象流:其中没有可重置的对象。 It is also obviously the source of this problem. 这显然也是此问题的根源。 Just remove it. 只需将其删除。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM