Hello, I tried to serialise Objects using ObjectOutputStream.writeObject. And got the output as bytes using the ByteArrayOutputStream. But I have found the bytes I got from the ByteArrayOutputStream is different to the output to a file on the harddisk using FileOutputStream.
And when I tried to retrieve the object from the bytes I got from the ByteArrayOutputStream. I got:
java.io.StreamCorruptedException: invalid stream header when the bytes are sent accrossed the network and is about to be retrieved by a different node.
whereas I can successfully retrieve the object from the bytes written to the file using FileOutputStream.
So I am wondering how I can seriliaze objects to bytes and retrieves the bytes from another running program on another JVM. I am restricted to TCP connection and transporting data as bytes.
That's some odd behavior. ObjectOutputStream writes a header to the stream so that the input stream has some clue as to how to reinstantiate the original object. I wouldn't think that there would be a different header for a file as opposed to an in-memory byte stream (they're pretty much the same thing). Is there any reason you are using a ByteArrayOutputStream as opposed to writing the object directly to the socket using ObjectArrayOutputStream? Can you share some small snippets of code so we can see what you are doing?