How to convert a byte array to ObjectInputStream

A

Ahmed Moustafa

I'm trying to use

<code>
byte[] array = {1, 2};
ByteArrayInputStream bis = new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(bis);
</code>

But it throws exception at the 3rd line:
java.io.StreamCorruptedException: invalid stream header

Your help is appreciated!
 
G

Gordon Beaton

I'm trying to use

<code>
byte[] array = {1, 2};
ByteArrayInputStream bis = new ByteArrayInputStream(array);
ObjectInputStream ois = new ObjectInputStream(bis);
</code>

But it throws exception at the 3rd line:
java.io.StreamCorruptedException: invalid stream header

Your help is appreciated!

You can't just put a couple of random numbers into an array and expect
to somehow read objects from it.

Before you can create an ObjectInputStream from the byte array, you
need to fill it with the header written by an ObjectOutputStream
constructor, and optionally one or more serialized objects.

The exception says that the header is missing (or corrupt).

/gordon
 
A

Ahmed Moustafa

Gordon said:
You can't just put a couple of random numbers into an array and expect
to somehow read objects from it.

Before you can create an ObjectInputStream from the byte array, you
need to fill it with the header written by an ObjectOutputStream
constructor, and optionally one or more serialized objects.

The exception says that the header is missing (or corrupt).

I added ObjectStreamConstants.TC_BLOCKDATA and
ObjectStreamConstants.TC_ENDBLOCKDATA to the byte array but still it
does work.
 
G

Gordon Beaton

I added ObjectStreamConstants.TC_BLOCKDATA and
ObjectStreamConstants.TC_ENDBLOCKDATA to the byte array but still it
does work.

Does or doesn't?

An ObjectInputStream serves no purpose except to reconstitute objects
previously serialized with an ObjectOutputStream. There is little
point in attempting to read objects from an array containing some
apparently random numbers.

Create an ObjectOutputStream around a ByteArrayOutputStream. Close the
ObjectOutputStream. Now the resulting byte array will contain a valid
header for the ObjectInputStream.

If you want to read actual objects as well, you need to write them to
the ObjectOutputStream before closing it.

/gordon
 
A

Ahmed Moustafa

Gordon said:
Does or doesn't?

Yes, it didn't work.
An ObjectInputStream serves no purpose except to reconstitute objects
previously serialized with an ObjectOutputStream. There is little
point in attempting to read objects from an array containing some
apparently random numbers.

Create an ObjectOutputStream around a ByteArrayOutputStream. Close the
ObjectOutputStream. Now the resulting byte array will contain a valid
header for the ObjectInputStream.

If you want to read actual objects as well, you need to write them to
the ObjectOutputStream before closing it.

I did that and I got the exception OptionalDataException. What does this
exception mean?
 
G

Gordon Beaton

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top