Is this an error in the Java gzipinputstream class?

I noticed that some of my gzip decoding code didn't seem to detect corrupted data I think I've traced the problem back to the Java gzipinputstream class In particular, when you use a single "read" call to read the entire stream, corrupted data does not trigger IOException If you read streams in 2 or more calls in the same corrupted data, an exception is thrown

Before considering submitting an error report, I want to see what the community here is

Editor: I revised my example because the last one didn't clearly explain what I thought was the problem In this new example, a 10 byte buffer is compressed by gzip, and one byte of the gzip compressed buffer is modified and decompressed For 'gzipinputstream The call to 'read' returns 10 as the number of bytes read, which is the 10 byte buffer you expect However, the decompressed buffer is different from the original buffer (due to corruption) Throw an exception I do notice that after reading, if the EOF has arrived, it returns' 1 'instead of' 0 '

Source:

@Test public void gzip() {
    try {
      int length = 10;
      byte[] bytes = new byte[]{12,19,111,14,-76,34,60,-43,-91,101};
      System.out.println(Arrays.toString(bytes));

      //Gzip the byte array
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      GZIPOutputStream gos = new GZIPOutputStream(baos);
      gos.write(bytes);
      gos.finish();
      byte[] zipped = baos.toByteArray();

      //Alter one byte of the gzipped array.  
      //This should be detected by gzip crc-32 checksum
      zipped[15] = (byte)(0);

      //Unzip the modified array
      ByteArrayInputStream bais = new ByteArrayInputStream(zipped);
      GZIPInputStream gis = new GZIPInputStream(bais);
      byte[] unzipped = new byte[length];
      int numRead = gis.read(unzipped);
      System.out.println("NumRead: " + numRead);
      System.out.println("Available: " + gis.available());

      //The unzipped array is Now [12,-80,10,-118].
      //No IOException was thrown.
      System.out.println(Arrays.toString(unzipped));

      //Assert that the input and unzipped arrays are equal (they aren't)
      org.junit.Assert.assertArrayEquals(unzipped,bytes);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

Solution

Decide to run the test:

What did you miss Read (unzipped) returns 1, so it reads only one byte You can't complain. This is not the end of the flow

The next read() throws a "broken gzip Trailer."

So it's all good! (and there are no bugs, at least in gzipinputstream)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>