简体   繁体   English

GZipInputStream .read()将零插入缓冲区

[英]GZipInputStream .read() insert zeros into buffer

I am having a strange program with a GzipInputStream zero filling part of the buffer. 我有一个奇怪的程序,缓冲区的GzipInputStream零填充。 I have the fortune of knowing what the bytes are supposed to look like in the stream and I can see the buffer is being filled with 8 correct bytes and 12 zeros (shouldn't be zero) 我很幸运知道流中的字节应该是什么样子,并且可以看到缓冲区中充满了8个正确的字节和12个零(不应为零)。

BYTES SHOULD LOOK LIKE THIS----> 0 20 82 22 -91 27 -96 65 66 65 88 32 32 32 32 81 32 0 0 0 100 78 字节应该像这样-> 0 20 82 22 -91 27 -96 65 66 65 88 32 32 32 32 81 32 0 0 0 100 78

BYTES ACTUALLY LOOK LIKE THIS---> 0 20 82 22 -91 27 -96 65 66 65 0 0 0 0 0 0 0 0 0 0 0 0 字节实际上看起来像这样-> 0 20 82 22 -91 27 -96 65 66 65 0 0 0 0 0 0 0 0 0 0 0 0

The first two bytes represent an integer that determine the size of the variable length (in bytes) payload after the first two. 前两个字节代表一个整数,该整数确定前两个字节后可变长度有效载荷的大小(以字节为单位)。 So in this example, the first bytes are 0 20 and in BIG_ENDIAN this gives us a subsequent payload size of 20 bytes. 因此,在此示例中,第一个字节为0 20,在BIG_ENDIAN中,这为我们提供了20字节的后续有效负载大小。

Here is my code for reading 这是我的阅读代码

gzipInputStream = new GZIPInputStream(url.openStream());        

byte[] payload = new byte[2];
gzipInputStream.read(payload);
for(int i=0;i<payload.length;i++){
    System.out.println(payload[i]);
}
int payloadSize = ((payload[0] & 0xFF) << 8) | ((payload[1]) & 0xFF);
//read the next payloadSize bytes
byte[] messageBytes = new byte[payloadSize];

gzipInputStream.read(messageBytes);

So the first two bytes are those in the payload array and the second 20 bytes are those in messageBytes. 因此,前两个字节是有效负载数组中的字节,后20个字节是messageBytes中的字节。 Can't figure it out 无法弄清楚

Modified code thanks to NPE 借助NPE修改了代码

            byte[] payloadSizeBytes = new byte[2];
            int payloadSizeBytesRead = 0;
            while(payloadSizeBytesRead < 2){
                int r = gzipInputStream.read(buffer);
                if(r>0){
                    payloadSizeBytes[payloadSizeBytesRead] = buffer[0];
                    payloadSizeBytesRead++;
                }
            }
            int payloadSize = ((payloadSizeBytes[0] & 0xFF) << 8) | ((payloadSizeBytes[1]) & 0xFF);
            //read the next payloadSize bytes
            byte[] messageBytes = new byte[payloadSize];
            int messageBytesRead = 0;
            while(messageBytesRead < payloadSize){
                int r = gzipInputStream.read(buffer);
                if(r>0){
                    messageBytes[messageBytesRead] = buffer[0];
                    messageBytesRead++;
                }
            }
            for(int i=0;i<messageBytes.length;i++){
                System.out.println(messageBytes[i]);
            }

The contract on read(byte[]) is that it reads some data, and returns how many bytes have been read. read(byte[])的约定是,它读取一些数据,并返回已读取的字节数。 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. 相反,您应该检查read()的返回值,并继续调用read()直到您读取了payloadSize字节为止。

An easy way to do this is by using read(b, off, len) in a loop: 一种简单的方法是在循环中使用read(b, off, len)

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM