Reputation: 5532
I'm trying to serialize/deserialize a bitmap. The answers at android how to save a bitmap - buggy code are very helpful. However, when I go to read my array:
private void readObject(ObjectInputStream in) throws IOException,
ClassNotFoundException {
int rowBytes = in.readInt();
int height = in.readInt();
int width = in.readInt();
int bmSize = rowBytes * height; // Ends up being 398208
ByteBuffer byteBuffer = ByteBuffer.allocate(bmSize);
int bytesRead = in.read(byteBuffer.array(), 0, bmSize);
// Use array to create bitmap here
}
it is reading 1008 bytes, not the 398208 bytes that I wrote. I've replaced the call with a loop, which works fine:
for (int i = 0; i < bmSize; i++) {
byteBuffer.array()[i] = in.readByte();
}
What could be going wrong? No exception is thrown. The documentation for ObjectInputStream.read(byte[], int, int) indicates the only reason it should return early is if it hits the end of the stream, which clearly it isn't because my work-around doesn't throw any exceptions.
Upvotes: 0
Views: 1132
Reputation: 1307
The documentation is wrong. ObjectInputStream just calls inputStream to read the bytes. Use readFully if you want it to block until the data is read.
Upvotes: 2