user1651914
user1651914

Reputation: 183

Writing to the ObjectOutputStream and getting rubbish back

I put a number of objects into the stream, then get byte array out of it and then read everything back. First two pieces of data arrive in good condition, then I get zeroes and then EOF exception. Why?

try {
     ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
     ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
     objectOutputStream.writeObject("abcdef");
     objectOutputStream.writeInt(1);
     objectOutputStream.writeObject(new byte[]{1,2,3,4,5,6,7,8});
     objectOutputStream.writeInt(2);
     objectOutputStream.writeObject(new byte[]{11,12,13,14,15,16,17,18});
     objectOutputStream.close();


     byte[] original = byteArrayOutputStream.toByteArray();
     System.out.println(Arrays.toString(original));

     byte[] b=new byte[8];
     ObjectInputStream objectInputStream = new ObjectInputStream(new ByteArrayInputStream(original));
     String s= (String) objectInputStream.readObject(); // works fine
     objectInputStream.readInt(); // works fine

     objectInputStream.read(b); // why it reads zeroes instead of [1,2,3,4,5,6,7,8]?
     System.out.println(Arrays.toString(b));
     int length = objectInputStream.readInt(); // EOF unexpectedly reached, why?
     objectInputStream.read(b);
}
catch (IOException e) {
     e.printStackTrace();
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

Exception:

java.io.EOFException
at java.io.DataInputStream.readInt(DataInputStream.java:375)
at java.io.ObjectInputStream$BlockDataInputStream.readInt(ObjectInputStream.java:2775)
at java.io.ObjectInputStream.readInt(ObjectInputStream.java:949)

Upvotes: 1

Views: 1291

Answers (3)

Costi Ciudatu
Costi Ciudatu

Reputation: 38195

Have a look at how ObjectOutputStream handles arrays in writeObject (specifically, look at ObjectOutputStream.writeArray(). It writes an array marker byte (to know what kind of object is stored), then the array length and then the array elements themselves.

Long story short, you should read it back with readObject, not with plain read (unless you want to actually parse that stream of bytes yourself, not expecting your bytes to be retrieved as in the original byte array).

InputStream.read(byte[]) simply gets the raw stream of bytes and writes them in your buffer.

Moreover, the subsequent calls (other than mere read()s) are expected to fail because the pointer is not placed at the beginning of a data structure which the deserializer can understand.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

As well as closing your buffered stream...

You want to have readInt() for each writeInt() (as you are doing) You also need to have readObject() for each writeObject().

If you don't read the same way your wrote, you can't expect it to make any sense.

You are doing

objectOutputStream.writeObject(new byte[]{1,2,3,4,5,6,7,8});

so you need to do

byte[] bytes = (byte[]) objectInputStream.readObject();

Upvotes: 3

Brian Agnew
Brian Agnew

Reputation: 272297

I suspect that the first issue is that you need to close() your objectOutputStream. See the ObjectOutputStream documentation for examples.

Upvotes: 5

Related Questions