BrotliCompressor { public final int compress(Brotli.Parameter parameter, byte[] in, byte[] out) throws BrotliException { return compress(parameter, in, 0, in.length, out, 0, out.length); }  final int compress(Brotli.Parameter parameter, byte[] in, byte[] out); final int compress(Brotli.Parameter parameter, byte[] in, int inPosition, int inLength, byte[] out, int outPosition, int outLength); final int compress(Brotli.Parameter parameter, ByteBuffer in, ByteBuffer out);  }
@Test public void compress_with_byte_array() throws Exception { byte[] out = new byte[2048]; int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, 0, A_BYTES.length, out, 0, out.length); assertThat(outLength).isEqualTo(10); assertThat(out).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_using_position_and_length_on_input_array() throws Exception { byte[] in = createFilledByteArray(100, 'x'); byte[] out = new byte[2048]; int testPosition = 23; int testLength = A_BYTES.length; System.arraycopy(A_BYTES, 0, in, testPosition, testLength); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, in, testPosition, testLength, out, 0, out.length); assertThat(outLength).isEqualTo(10); assertThat(out).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_using_position_and_length_on_output_array() throws Exception { byte[] out = new byte[2048]; int testPosition = 23; int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, 0, A_BYTES.length, out, testPosition, A_BYTES_COMPRESSED.length); assertThat(outLength).isEqualTo(10); byte[] outCopiedRange = Arrays.copyOfRange(out, testPosition, testPosition + A_BYTES_COMPRESSED.length); assertThat(outCopiedRange).isEqualTo(A_BYTES_COMPRESSED); }
@Test(expectedExceptions = IllegalArgumentException.class) public void compress_with_input_byte_array_using_negative_position_throws_IllegalArgumentException() throws Exception { byte[] out = new byte[2048]; compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, -1, A_BYTES.length, out, 0, out.length); }
@Test(expectedExceptions = IllegalArgumentException.class) public void compress_with_output_byte_array_using_negative_position_throws_IllegalArgumentException() throws Exception { byte[] out = new byte[2048]; compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, 0, A_BYTES.length, out, -1, out.length); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Brotli: input array position and length must be greater than zero.") public void compress_with_input_byte_array_using_negative_length_throws_IllegalArgumentException() throws Exception { byte[] out = new byte[2048]; compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, 0, -1, out, 0, out.length); }
@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "Brotli: output array position and length must be greater than zero.") public void compress_with_output__byte_array_using_negative_length_throws_IllegalArgumentException() throws Exception { byte[] out = new byte[2048]; compressor.compress(Brotli.DEFAULT_PARAMETER, A_BYTES, 0, A_BYTES.length, out, 0, -1); }
@Test public void compress_with_direct_ByteBuffer() throws Exception { ByteBuffer inBuffer = wrapDirect(A_BYTES); ByteBuffer outBuffer = allocateDirect(10); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(10); assertThat(getByteArray(outBuffer)).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_direct_ByteBuffer_using_position_and_limit_on_input_buffer() throws Exception { ByteBuffer inBuffer = allocateDirect(100); ByteBuffer outBuffer = allocateDirect(A_BYTES_COMPRESSED.length); inBuffer.put(createFilledByteArray(100, 'x')); int testPosition = 23; inBuffer.position(testPosition); inBuffer.put(A_BYTES); inBuffer.limit(testPosition + A_BYTES.length); inBuffer.position(testPosition); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(0); assertThat(outBuffer.limit()).isEqualTo(outLength); assertThat(inBuffer.position()).isEqualTo(testPosition + A_BYTES.length); byte[] buf = new byte[A_BYTES_COMPRESSED.length]; outBuffer.get(buf); assertThat(buf).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_direct_ByteBuffer_using_position_and_limit_on_output_buffer() throws Exception { ByteBuffer inBuffer = allocateDirect(A_BYTES.length); ByteBuffer outBuffer = allocateDirect(100); inBuffer.put(A_BYTES); inBuffer.position(0); int testPosition = 23; outBuffer.position(testPosition); outBuffer.limit(testPosition + A_BYTES_COMPRESSED.length); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(testPosition); assertThat(outBuffer.limit()).isEqualTo(testPosition + outLength); assertThat(inBuffer.position()).isEqualTo(A_BYTES.length); byte[] buf = new byte[A_BYTES_COMPRESSED.length]; outBuffer.get(buf); assertThat(buf).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_wrapped_ByteBuffer() throws Exception { ByteBuffer inBuffer = wrap(A_BYTES); ByteBuffer outBuffer = allocate(10); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(10); byte[] buf = new byte[10]; outBuffer.get(buf); assertThat(buf).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_wrapped_ByteBuffer_using_position_and_limit_on_input_buffer() throws Exception { ByteBuffer inBuffer = wrap(createFilledByteArray(100, 'x')); ByteBuffer outBuffer = allocate(A_BYTES_COMPRESSED.length); int testPosition = 23; inBuffer.position(testPosition); inBuffer.put(A_BYTES); inBuffer.limit(testPosition + A_BYTES.length); inBuffer.position(testPosition); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(0); assertThat(outBuffer.limit()).isEqualTo(outLength); assertThat(inBuffer.position()).isEqualTo(testPosition + A_BYTES.length); byte[] buf = new byte[A_BYTES_COMPRESSED.length]; outBuffer.get(buf); assertThat(buf).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_wrapped_ByteBuffer_using_arrayOffset_and_limit_on_input_buffer() throws Exception { ByteBuffer inBuffer = wrap(createFilledByteArray(100, 'x')); ByteBuffer outBuffer = allocate(A_BYTES_COMPRESSED.length); int testPosition = 23; inBuffer.position(testPosition); inBuffer.put(A_BYTES); inBuffer.position(testPosition); inBuffer = inBuffer.slice(); inBuffer.limit(A_BYTES.length); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(0); assertThat(outBuffer.limit()).isEqualTo(outLength); assertThat(inBuffer.position()).isEqualTo(A_BYTES.length); byte[] buf = new byte[A_BYTES_COMPRESSED.length]; outBuffer.get(buf); assertThat(buf).startsWith(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_wrapped_ByteBuffer_using_position_and_limit_on_output_buffer() throws Exception { ByteBuffer inBuffer = wrap(A_BYTES); ByteBuffer outBuffer = allocate(100); int testPosition = 23; outBuffer.position(testPosition); outBuffer.limit(testPosition + A_BYTES_COMPRESSED.length); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(testPosition); assertThat(outBuffer.limit()).isEqualTo(testPosition + outLength); assertThat(inBuffer.position()).isEqualTo(A_BYTES.length); assertThat(getByteArray(outBuffer)).isEqualTo(A_BYTES_COMPRESSED); }
@Test public void compress_with_byte_array_wrapped_ByteBuffer_using_arrayOffset_and_limit_on_output_buffer() throws Exception { ByteBuffer inBuffer = wrap(A_BYTES); ByteBuffer outBuffer = allocate(100); int testPosition = 23; outBuffer.position(testPosition); outBuffer = outBuffer.slice(); outBuffer.limit(A_BYTES_COMPRESSED.length); int outLength = compressor.compress(Brotli.DEFAULT_PARAMETER, inBuffer, outBuffer); assertThat(outLength).isEqualTo(A_BYTES_COMPRESSED.length); assertThat(outBuffer.position()).isEqualTo(0); assertThat(outBuffer.limit()).isEqualTo(outLength); assertThat(inBuffer.position()).isEqualTo(A_BYTES.length); assertThat(getByteArray(outBuffer)).isEqualTo(A_BYTES_COMPRESSED); }