@Test public void testInvalidCompressionLevel() { final GzipParameters parameters = new GzipParameters(); try { parameters.setCompressionLevel(10); fail("IllegalArgumentException not thrown"); } catch (final IllegalArgumentException e) { // expected } try { parameters.setCompressionLevel(-5); fail("IllegalArgumentException not thrown"); } catch (final IllegalArgumentException e) { // expected } }
private void testExtraFlags(final int compressionLevel, final int flag) throws Exception { byte[] content; try (FileInputStream fis = new FileInputStream(getFile("test3.xml"))) { content = IOUtils.toByteArray(fis); } final ByteArrayOutputStream bout = new ByteArrayOutputStream(); final GzipParameters parameters = new GzipParameters(); parameters.setCompressionLevel(compressionLevel); final GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters); IOUtils.copy(new ByteArrayInputStream(content), out); out.flush(); out.close(); assertEquals("extra flags (XFL)", flag, bout.toByteArray()[8]); }
@Test public void testInteroperabilityWithGZIPInputStream() throws Exception { byte[] content; try (FileInputStream fis = new FileInputStream(getFile("test3.xml"))) { content = IOUtils.toByteArray(fis); } final ByteArrayOutputStream bout = new ByteArrayOutputStream(); final GzipParameters parameters = new GzipParameters(); parameters.setCompressionLevel(Deflater.BEST_COMPRESSION); parameters.setOperatingSystem(3); parameters.setFilename("test3.xml"); parameters.setComment("Test file"); parameters.setModificationTime(System.currentTimeMillis()); final GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters); out.write(content); out.flush(); out.close(); final GZIPInputStream in = new GZIPInputStream(new ByteArrayInputStream(bout.toByteArray())); final byte[] content2 = IOUtils.toByteArray(in); Assert.assertArrayEquals("uncompressed content", content, content2); }
@Test public void testMetadataRoundTrip() throws Exception { final ByteArrayOutputStream bout = new ByteArrayOutputStream(); final GzipParameters parameters = new GzipParameters(); parameters.setCompressionLevel(Deflater.BEST_COMPRESSION); parameters.setModificationTime(123456000); parameters.setOperatingSystem(13); parameters.setFilename("test3.xml"); parameters.setComment("Umlaute möglich?"); try (GzipCompressorOutputStream out = new GzipCompressorOutputStream(bout, parameters); FileInputStream fis = new FileInputStream(getFile("test3" + ".xml"))) { IOUtils.copy(fis, out); } final GzipCompressorInputStream input = new GzipCompressorInputStream(new ByteArrayInputStream(bout.toByteArray())); input.close(); final GzipParameters readParams = input.getMetaData(); assertEquals(Deflater.BEST_COMPRESSION, readParams.getCompressionLevel()); assertEquals(123456000, readParams.getModificationTime()); assertEquals(13, readParams.getOperatingSystem()); assertEquals("test3.xml", readParams.getFilename()); assertEquals("Umlaute möglich?", readParams.getComment()); }