这是Java GZipInputStream类中的错误吗?
发布时间:2020-12-14 05:18:45 所属栏目:Java 来源:网络整理
导读:我注意到我的一些gzip解码代码似乎没有检测到损坏的数据.我认为我已经将问题追溯到 Java GZipInputStream类.特别是,当您使用单个“读
我注意到我的一些gzip解码代码似乎没有检测到损坏的数据.我认为我已经将问题追溯到
Java GZipInputStream类.特别是,当您使用单个“读取”调用读取整个流时,损坏的数据不会触发IOException.如果您在同一损坏的数据中读取2个或更多个调用中的流,则会引发异常.
在考虑提交错误报告之前,我想看看这里的社区是什么. 编辑:我修改了我的例子,因为最后一个没有清楚地说明我认为是什么问题.在这个新的例子中,一个10字节的缓冲区被gzip压缩,gzip压缩缓冲区的一个字节被修改,然后被解压缩.对’GZipInputStream.read’的调用返回10作为读取的字节数,这是您期望的10字节缓冲区.然而,解压缩的缓冲区与原始缓冲区不同(由于损坏).抛出异常.我确实注意到,读取后,如果EOF已经到达,则返回’1’而不是’0′. 来源: @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(); } } 解决方法
决定运行测试:
你错过了什么 下一个read()抛出“损坏的GZIP预告片”. 所以这一切都很好! (并且没有bug,至少在GZIPInputStream中) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |