GZIPInputStream not reading fully?

I’m trying to send GZipped map data over the network. For some reason, this code only reads 37747 bytes rather than the needed amount (5 * 512 * 512 in this case).


                    int width = bb.getInt();
                    int height = bb.getInt();
                    byte[] arr = mapCache.array(); //This is an array of GZIPped data
                    ByteArrayInputStream bIn = new ByteArrayInputStream(arr);
                    GZIPInputStream gzIn = new GZIPInputStream(bIn);
                    byte[] levelBytes = new byte[5 * (width * height)];
                    System.err.println(gzIn.read(levelBytes, 0, 5 * (width * height))); //37747
                    gzIn.close();
                    bIn.close();

I tried redirecting mapCache to a file and opening it with 7Zip, and got the complete level.

Thanks for any help.

EDIT: Answered my own, question, read() isn’t supposed to read fully the first time.

For the sake of completeness, it can require N calls to read(byte[]) to fill a buffer of N bytes.

If you want the easy way out, use DataInputStream:


ByteArrayInputStream bIn = new ByteArrayInputStream(arr);
GZIPInputStream gzIn = new GZIPInputStream(bIn);
DataInputStream dIn = new DataInputStream(gzIn);

dIn.readFully(levelBytes);