0

I'm trying to write to and read from one file with ObjectInputStream and ObjectOutputStream, in two threads. 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.

  • @close-voter There may not be enough information here for you, but there is for me. Please don't vote on things that you don't understand. – user207421 Apr 09 '14 at 04:14
  • I don't know what you mean. If somebody really solved my problem, why can't I give him a "useful" vote? – user3513356 Apr 09 '14 at 06:26
  • The comment is specifically not addressed to you. It was addressed to whoever voted to close this question without understanding it. Vote as you wish. – user207421 Apr 09 '14 at 07:14
  • Sorry that my reputation is not enough to give you an up vote. But you indeed solve my problem. Thank you. – user3513356 Apr 11 '14 at 02:50

1 Answers1

0

Calling reset() in writeStreamHeader() is completely pointless. 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.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • Thanks very much. After 2 days testing, the problem seems to be fixed.But what really confuses me is some documents, like this one, [link](http://stackoverflow.com/questions/1194656/appending-to-an-objectoutputstream), they addressed that we need to put `reset()` in the `writeHeaderStream()`, or there would be some problems. But for my case, if I add `reset()`, there does be some problems. Isn't there a rather better solution? – user3513356 Apr 11 '14 at 02:44