Question

is there a way to force ObjectOutputStream not to rely on the previous writes, that should be to write Object properties and full info each time?

I can see from the binary file that is writes only some kind of increment from previous version and if you try to load random item it will fail?

Was it helpful?

Solution

Yes, you want ObjectOutputStream.reset()

public void reset() throws IOException

Reset will disregard the state of any objects already written to the stream. The state is reset to be the same as a new ObjectOutputStream. The current point in the stream is marked as reset so the corresponding ObjectInputStream will be reset at the same point. Objects previously written to the stream will not be refered to as already being in the stream. They will be written to the stream again.

Earlier, the Javadoc discusses writing references to already seen objects:

The default serialization mechanism for an object writes the class of the object, the class signature, and the values of all non-transient and non-static fields. References to other objects (except in transient or static fields) cause those objects to be written also. Multiple references to a single object are encoded using a reference sharing mechanism so that graphs of objects can be restored to the same shape as when the original was written.

As the reset Javadoc explains, calling reset causes the stream state to be reset. You should be aware that this reset operation itself writes bytes to the stream (so that the decoding end also knows when to reset the stream).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top