The contract on read(byte[])
is that it reads some data, and returns how many bytes have been read. As things stand, you are ignoring the return value. Instead, you should examine the return value of read()
and keep calling read()
until you've read payloadSize
bytes.
An easy way to do this is by using read(b, off, len)
in a loop:
int payloadSize = ((payload[0] & 0xFF) << 8) | ((payload[1]) & 0xFF);
byte[] messageBytes = new byte[payloadSize];
int bytesRead = 0;
while (bytesRead < payloadSize) {
bytesRead += gzipInputStream.read(messageBytes, bytesRead, payloadSize - bytesRead);
}