BrotliInputStream extends InputStream { @Override public int read() throws IOException { if (uncompressedInputStreamDelegate.available() == 0) { readChunkFromInput(); } return uncompressedInputStreamDelegate.read(); } BrotliInputStream(InputStream inputStream); @Override int read(); @Override int read(byte[] b); @Override int read(byte[] b, int off, int len); @Override int available(); @Override void close(); @Override boolean markSupported(); @Override long skip(long n); @Override synchronized void mark(int readlimit); @Override synchronized void reset();  }
@Test public void read_byte_array_fully_with_larger_buffer() throws Exception { byte[] outBuf = new byte[A_BYTES.length + 100]; int read = brotliInputStream.read(outBuf); assertThat(read).isEqualTo(A_BYTES.length); assertThat(outBuf).startsWith(A_BYTES); read = brotliInputStream.read(outBuf); assertThat(read).isLessThanOrEqualTo(0); }
@Test public void read_huge_byte_array_which_exceeds_internal_buffer_size() throws Exception { byte[] a_bytes = BufferTestHelper.createFilledByteArray(INTERNAL_UNCOMPRESSED_BUFFER_SIZE * 2, 'a'); byte[] a_bytes_compressed = brotliCompress(a_bytes); byte[] readBuffer; brotliInputStream = new BrotliInputStream(new ByteArrayInputStream(a_bytes_compressed)); readBuffer = new byte[INTERNAL_UNCOMPRESSED_BUFFER_SIZE + 1]; int read1 = brotliInputStream.read(readBuffer); assertThat(read1).isEqualTo(readBuffer.length); assertBufferContainsOnlySingleCharacter(readBuffer, 0, read1, 'a'); readBuffer = new byte[INTERNAL_UNCOMPRESSED_BUFFER_SIZE + 1]; int read2 = brotliInputStream.read(readBuffer); assertThat(read2).isEqualTo(readBuffer.length - 2); assertBufferContainsOnlySingleCharacter(readBuffer, 0, read2, 'a'); }
@Test public void byte_wise_decompression_works() throws Exception { byte[] uncompressedBuffer = new byte[A_BYTES.length]; for (int i = 0; i < uncompressedBuffer.length; i++) { uncompressedBuffer[i] = (byte) brotliInputStream.read(); } assertThat(uncompressedBuffer).isEqualTo(A_BYTES); }
@Test public void byte_wise_decompression_signals_end_of_stream() throws Exception { readBrotliStreamFully(); int read = brotliInputStream.read(); assertThat(read).isEqualTo(-1); }
@Test public void read_byte_array_fully() throws Exception { byte[] outBuf = new byte[A_BYTES.length]; int read = brotliInputStream.read(outBuf); assertThat(read).isEqualTo(A_BYTES.length); assertThat(outBuf).isEqualTo(A_BYTES); }