PartiallyUncompressingPipe implements Closeable { public long pipe(InputStream in, Mode mode) throws IOException { long bytesWrittenBefore = out.getNumBytesWritten(); if (mode == Mode.COPY) { int numRead = 0; while ((numRead = in.read(copyBuffer)) >= 0) { out.write(copyBuffer, 0, numRead); } } else { uncompressor.setNowrap(mode == Mode.UNCOMPRESS_NOWRAP); uncompressor.uncompress(in, out); } out.flush(); return out.getNumBytesWritten() - bytesWrittenBefore; } PartiallyUncompressingPipe(OutputStream out, int copyBufferSize); long pipe(InputStream in, Mode mode); long getNumBytesWritten(); @Override void close();  }
@Test public void testWriteAll_Uncompressed() throws IOException { byte[] expectedBytes = new byte[] {1, 2, 3, 4, 5}; stream.pipe(new ByteArrayInputStream(expectedBytes), Mode.COPY); Assert.assertArrayEquals(expectedBytes, outBuffer.toByteArray()); }
@Test public void testWriteAll_Compressed_NoWrapTrue() throws IOException { UnitTestZipEntry entry = UnitTestZipArchive.makeUnitTestZipEntry("/foo", 7, "frobozz", null); stream.pipe( new ByteArrayInputStream(entry.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP); Assert.assertArrayEquals(entry.getUncompressedBinaryContent(), outBuffer.toByteArray()); }
@Test public void testWriteAll_Compressed_NoWrapFalse() throws IOException { UnitTestZipEntry entry = UnitTestZipArchive.makeUnitTestZipEntry("/foo", 6, "frobozz", null); DeflateCompressor compressor = new DeflateCompressor(); compressor.setNowrap(false); ByteArrayOutputStream compressBuffer = new ByteArrayOutputStream(); compressor.compress( new ByteArrayInputStream(entry.getUncompressedBinaryContent()), compressBuffer); stream.pipe(new ByteArrayInputStream(compressBuffer.toByteArray()), Mode.UNCOMPRESS_WRAPPED); Assert.assertArrayEquals(entry.getUncompressedBinaryContent(), outBuffer.toByteArray()); }
@Test public void testWriteAll_Multiple() throws IOException { UnitTestZipEntry entryA = UnitTestZipArchive.makeUnitTestZipEntry("/bar", 3, "dragon lance", null); UnitTestZipEntry entryB = UnitTestZipArchive.makeUnitTestZipEntry("/baz", 8, "kender & hoopak", null); ByteArrayOutputStream expected = new ByteArrayOutputStream(); byte[] expectedBytes1 = new byte[] {1, 2, 3, 4, 5}; expected.write(expectedBytes1); stream.pipe(new ByteArrayInputStream(expectedBytes1), Mode.COPY); stream.pipe( new ByteArrayInputStream(entryA.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP); expected.write(entryA.getUncompressedBinaryContent()); byte[] expectedBytes3 = new byte[] {6, 7, 8, 9, 0}; stream.pipe(new ByteArrayInputStream(expectedBytes3), Mode.COPY); expected.write(expectedBytes3); stream.pipe( new ByteArrayInputStream(entryB.getCompressedBinaryContent()), Mode.UNCOMPRESS_NOWRAP); expected.write(entryB.getUncompressedBinaryContent()); byte[] expectedBytes5 = new byte[] {127, 127, 127, 127, 127, 127}; stream.pipe(new ByteArrayInputStream(expectedBytes5), Mode.COPY); expected.write(expectedBytes5); Assert.assertArrayEquals(expected.toByteArray(), outBuffer.toByteArray()); }