• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in de-Serialization

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".


java.io.StreamCorruptedException: invalid stream header
----------------------------------------------------

----------------------------------------------------

java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:767)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:283)
at com.lxnx.ols.rosetta.history.shared.common.HistoryUtilsImpl.convertDOList(HistoryUtilsImpl.java:397)


The data was written like this

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

oos.close();


Anybody has any idea about the fix for this?
It would be helpful if you can send a sample piece of code.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
reply
    Bookmark Topic Watch Topic
  • New Topic