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

Exception in de-Serialization  RSS feed

Vilas Kulkarni
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See piece of code,

ByteArrayInputStream bis = new ByteArrayInputStream(objData.getBytes("ISO-8859-1"));
ObjectInputStream ois = new ObjectInputStream(bis);

I get this exception while trying to initialize the above "ObjectInputStream". invalid stream header

---------------------------------------------------- invalid stream header
at com.lxnx.ols.rosetta.history.shared.common.HistoryUtilsImpl.convertDOList(

The data was written like this

ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
String objData = baos.toString("ISO-8859-1");
String encryptSrchParamStr = LnAes.encrypt(objData);


Anybody has any idea about the fix for this?
It would be helpful if you can send a sample piece of code.
Jim Yingst
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the problem is here:

From the API for this method: "This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement string." The problem is that not all possible byte values are legal values in ISO-8859-1, and those byte values are getting replaced by a ? or something similar. Once this happens, the data is lost, and there's really no way you can recover it.

More generally, it's usually a bad idea to try to take arbitrary binary data (like the output of an ObjectOutputStream) and try to write it as a String of any sort. It's very easy to get mangled data such as you have here. Doesn't your encryption too have a method for encrypting bytes rather than characters? I think any good encryption tool should have this ability, but I don't know what this LnAes thing is. I strongly recommend getting an encryption tool that allows you to encrypt raw bytes, not just strings.

If you absolutely must convert the data to a string before encrypting, you need to use an encoding in which all 256 possible byte values are legal values for characters. There probably is one, somewhere, but unfortunately I don't know what that encoding might be. Here's some test code for you:
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!