Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Gets the output of the ObjectOutputStream in a program  RSS feed

 
Lin Shen
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Ess
Bartender
Posts: 9428
12
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!