This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
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’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com
 
subject: Serialization issue
 
It's not a secret anymore!