StreamCorruptedException while trying to parse a byte array in Java

I'm writing an Android app, trying to parse some binary data received via Bluetooth, containing various signed and unsigned 1-4 byte integers. I'm given to understand that the best (or possibly only) way to interpret bytes as unsigned numbers is via an input stream. My code is:

ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis);

The second line, for some reason, throws a StreamCorruptedException with no data in it. What am I doing wrong? Also how is it conceptually possible to "corrupt" a bunch of bytes?

Jon Skeet
people
quotationmark

Unless the data has been written by ObjectOutputStream, you shouldn't use ObjectInputStream - that's very specific to Java's binary serialization. If this is just binary data, 4 bytes for a 32-bit integer etc, then you probably want DataInputStream instead - but you should validate that your data is big-endian, which is what DataInputStream will handle it.

people

See more on this question at Stackoverflow