Thursday, March 12, 2009

Why can't a file that contains multiple appended ObjectOutputStreams be

Question :Why can't a file that contains multiple appended ObjectOutputStreams be
deserialized by one ObjectInputStream? (CoreJava)

Answer :Using the default implementation of serialization, there must be a one-toone
mapping between ObjectOutputStream construction and
ObjectInputStream construction. ObjectOutputStream constructor writes a
stream header andObjectInputStream reads this stream header. A
workaround is to subclass ObjectOutputStream and override
writeStreamHeader(). The overriding writeStreamHeader() should call the
super writeStreamHeader method if it is the first write to the file and it
should call ObjectOutputStream.reset() if it is appending to a pre-existing
ObjectOutputStream within the file.

No comments: