File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes StreamCorruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "StreamCorruptedException" Watch "StreamCorruptedException" New topic
Author

StreamCorruptedException

Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
HI all,
i am facing the below mentioned error when i tried to pass the byte array.

I am creating a XML and converting it to byte array before inserting into the DB, while retreivng i getting it as byte array and tried to view the object.

Any idea??


java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:737)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:253)
at com.cccis.interfaces.iatpa.reprocess.journal.JournalClient.convertBytesToObjectMsg(JournalClient.java:38)
at com.cccis.interfaces.iatpa.reprocess.journal.JournalClient.main(JournalClient.java:121)



i am posting the code snippet for your reference....

i am passing the byte array and i am getting the error listed above...

[ July 03, 2006: Message edited by: Parameswaran Thangavel ]
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
Have you tested Class.getName() to make sure that it is a byte[] being returned? Some databases crypt the data when being stored and need to be decrypted. example: Base64

Also is there a reason why you are manually doing this yourself and not using a wrapper with java.io.Serializable?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

This code looks fine; the problem is either on the other end (i.e., the code that encodes and inserts the bytes into the database) or it's the code that extracts the byte[] from the database.

As far as "testing getClass().getName()" -- I'm afraid that's not a very useful suggestion, given that there are no casts to or from byte[] here; and even if there were, using "instanceof byte[]" would be a far superior approach.


[Jess in Action][AskingGoodQuestions]
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi All
Thanks for your reply.

The defenition for ObjectInputStream says,
"Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified. This constructor will block until the corresponding ObjectOutputStream has written and flushed the header." I didn't understand what it meant by.

I completely have no idea what is mentioned there.
Can any one let me know...

Further, i created a simple string object and coverted it to byte array to check whether its working ... but it didn't help.


still it throws the same error.....
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi Nick
I am afraid to say that i didn't understand your point
"Also is there a reason why you are manually doing this yourself and not using a wrapper with java.io.Serializable?"

What i am doing here is Converting an XMLObject to byte array and store it in database using the service provided by service team. so i am unaware of how the byte array is get stored and usind the same service i am retrieving it back as XMLObject. Now i want to display this XML to the user. so i tried to conver the byte[] to Object. that where i faced the issue.

Hope this will help you all to understand my problem
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I wouldn't worry about Nick's point, whatever it was.

ObjectInputStreams can only read streams of bytes created by ObjectOutputStreams. String.getBytes() does not return a serialized form of the String object, and so if this is how you're encoding your data, there's the problem right there.
Parameswaran Thangavel
Ranch Hand

Joined: Mar 01, 2005
Posts: 485
Hi Ernest,
you r right.

The problem is with the way we are writing the data into the DB.

When converting the object to byte array, the best way to do is use both
ByteArrayInputStream and ObjectInputStream....

Thanks to all...
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
Ernest Friedman-Hill why all the targeted aggression? I was just relating to a JNDI issue when retrieving passwords from OpenLDAP database that we thought were byte[] but ended up needing to be decrypted from Base64.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

I apologize; I definitely should have been nicer.
Nick Meverden
Ranch Hand

Joined: Jul 03, 2006
Posts: 59
no worries
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: StreamCorruptedException