*
The moose likes Distributed Java and the fly likes Serialization issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Serialization issue" Watch "Serialization issue" New topic
Author

Serialization issue

Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
I am using my own serialization for some classes. The way in which I do is,


Is throwing a java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)

I cannot transfer raw bytes through the api I use for sending. The API is accepting only String, hence I tried to create a String from the bytes.
Any other alternatives?
And Why this problem? Any fault in code?
regards
Binu K Idicula
Binu K Idicula
Ranch Hand

Joined: Jul 11, 2002
Posts: 99
This is the answer which I got from my friend.
Posting it for info

When you created the byte[] (before sending) it has object headers as well as other serialization values. Those byte value of headers range from -128 to +127. The ObjectOutputStream outputs two intial headers having byte values -84 and -19, which is causing you the problem. When you first create a string specifying that byte[] is in UTF-8 encoding, it makes certain assumptions (thinking it is UTF-8) and does the transformation to String. Those values -84 & -19 won't come in a real UTF-8 byte system. So when you decode that, the bytes formulated are corrupted since the initial assumptions system made during encoding were wrong. So use the encoding "ISO-8859-1" for sending such values (don't use US-ASCII also because negetive value range is invalid in that also). So just change the encoding and decoding charsets to ISO-8859-1 and your program will work fine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Serialization issue
 
Similar Threads
Exception in finally
Convert java.io.File to ByteArryInputStream?
Reading From a Binary Large Object(BLOB)
Document doc => xml => blob at mySQL
Wanted to pack values in java like struct.pack() in python