简体   繁体   English

Java GZIPInputStream.read()函数

[英]Java GZIPInputStream.read() function

In the following line, when instream is a GZIPInputStream, I found that the values of c are totally random, either greater or less than 1024. But when instream is a FileInputStream, the returned value is always 1024. 在下一行中,当instream是GZIPInputStream时,我发现c的值是完全随机的,大于或小于1024。但是,当instream是FileInputStream时,返回的值始终为1024。

int c;
while ((c = instream.read(buffer, offset, 1024)) != -1)
    System.out.println("Bytes read: " + c);

The input source file size is much more than 1024 bytes. 输入源文件的大小远大于1024字节。 Why is the returned value of GZIPInputStream unpredictable? 为什么GZIPInputStream的返回值不可预测? Shouldn't it always read up to the said value 1024? 它不应该一直读取到上述值1024吗? Thanks! 谢谢!

It's just an artifact of compression. 这只是压缩的产物。 Typically a compressed block in a GZIP (which is variable in size) cannot be read unless the entirety of the block is decompressed. 通常,除非对整个块进行解压缩,否则无法读取GZIP中的压缩块(大小可变)。

You are reading blocks: 您正在阅读块:

0           1024           2048           3072           4096...

But if the compressed blocks' boundaries looks like this: 但是,如果压缩块的边界看起来像这样:

0       892     1201        2104         2924 ...

You're going to get a first read of 892 bytes, then 309 (1201-892), then 903 (2104-1201), etc. This is a slight over-simplification, but not much. 您将首先读取892个字节,然后读取309(1201-892),然后读取903(2104-1201),依此类推。这有点过分简化,但并不过分。

As Miserable Variable commented above, the read should never return MORE than 1024 otherwise that would imply a buffer overrun. 正如上面的Miserable Variable所评论的那样,读操作永远不要返回大于1024的值,否则将意味着缓冲区溢出。

No, the returned value does not need to be equal to 1024 - consider what should be returned in the case of aa file of size 4 bytes. 不,返回的值不必等于1024-考虑大小为4字节的文件应返回什么。 Always use the returned value for processing. 始终使用返回值进行处理。 Also, depending on the encoding type, it may be less than what you would expect due to circumstances out of your control (fe a network that only provides 512 bytes/sec). 另外,根据编码类型的不同,由于无法控制的情况(网络仅提供512字节/秒),它可能会比您期望的要小。

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

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