PartiallyCompressingOutputStream extends FilterOutputStream { @Override public void write(int b) throws IOException { internalCopyBuffer[0] = (byte) b; write(internalCopyBuffer, 0, 1); } PartiallyCompressingOutputStream(
      List<TypedRange<JreDeflateParameters>> compressionRanges,
      OutputStream out,
      int compressionBufferSize); @Override void write(int b); @Override void write(byte[] buffer); @Override void write(byte[] buffer, int offset, int length);  }
@Test public void testWrite_Nothing() throws IOException { stream = new PartiallyCompressingOutputStream( Collections.<TypedRange<JreDeflateParameters>>emptyList(), outBuffer, 32768); byte[] input = new byte[] {}; stream.write(input); stream.flush(); stream.close(); Assert.assertArrayEquals(input, outBuffer.toByteArray()); }
@Test public void testWrite_NoneCompressed() throws IOException { stream = new PartiallyCompressingOutputStream( Collections.<TypedRange<JreDeflateParameters>>emptyList(), outBuffer, 32768); byte[] input = new byte[] {1, 77, 66, 44, 22, 11}; byte[] expected = input.clone(); stream.write(input); stream.flush(); Assert.assertArrayEquals(expected, outBuffer.toByteArray()); }
@Test public void testWrite_AllCompressed() throws IOException { TypedRange<JreDeflateParameters> range = new TypedRange<JreDeflateParameters>( 0, ENTRY1.getUncompressedBinaryContent().length, PARAMS1); stream = new PartiallyCompressingOutputStream(Collections.singletonList(range), outBuffer, 32768); stream.write(ENTRY1.getUncompressedBinaryContent()); stream.flush(); Assert.assertArrayEquals(ENTRY1.getCompressedBinaryContent(), outBuffer.toByteArray()); }
@Test public void testWrite_GapAndCompression() throws IOException { stream = new PartiallyCompressingOutputStream( Collections.singletonList(COMPRESS_RANGE_1), outBuffer, 32768); byte[] input = fuse(PREAMBLE_BYTES, ENTRY1.getUncompressedBinaryContent()); byte[] expected = fuse(PREAMBLE_BYTES, ENTRY1.getCompressedBinaryContent()); stream.write(input); stream.flush(); Assert.assertArrayEquals(expected, outBuffer.toByteArray()); }
@Test public void testWrite_GapAndCompressionAndGap() throws IOException { stream = new PartiallyCompressingOutputStream( Collections.singletonList(COMPRESS_RANGE_1), outBuffer, 32768); byte[] input = fuse(PREAMBLE_BYTES, ENTRY1.getUncompressedBinaryContent(), GAP1_BYTES); byte[] expected = fuse(PREAMBLE_BYTES, ENTRY1.getCompressedBinaryContent(), GAP1_BYTES); stream.write(input); stream.flush(); Assert.assertArrayEquals(expected, outBuffer.toByteArray()); }
@Test public void testWrite_MixedSequence_Thrash() throws IOException { stream = new PartiallyCompressingOutputStream( Arrays.asList(COMPRESS_RANGE_1, COMPRESS_RANGE_2), outBuffer, 32768); byte[] input = fuse( PREAMBLE_BYTES, ENTRY1.getUncompressedBinaryContent(), GAP1_BYTES, ENTRY2.getUncompressedBinaryContent()); byte[] expected = fuse( PREAMBLE_BYTES, ENTRY1.getCompressedBinaryContent(), GAP1_BYTES, ENTRY2.getCompressedBinaryContent()); for (int x = 0; x < input.length; x++) { stream.write(input[x] & 0xff); stream.flush(); } stream.close(); Assert.assertArrayEquals(expected, outBuffer.toByteArray()); }