Skip to content

Commit

Permalink
SHELF implement finish()
Browse files Browse the repository at this point in the history
  • Loading branch information
nitram509 committed Jul 25, 2016
1 parent d7f5395 commit 91e63c6
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static java.lang.Math.min;
import static org.meteogroup.jbrotli.BrotliErrorChecker.assertBrotliOk;

public final class BrotliStreamCompressor implements Closeable {
public final class BrotliStreamCompressor implements Closeable, AutoCloseable {

static {
assertBrotliOk(initJavaFieldIdCache());
Expand Down Expand Up @@ -119,10 +119,11 @@ public final int getMaxInputBufferSize() throws BrotliException {
* Every stream must be finished, to create byte byte meta data according to the specification.
* If a stream is NOT finished, de-compressors are unable to parse a stream (find the end),
* which results in an error.
* This method also flushes the stream.
* @return the last bytes, to close the stream.
*/
public final byte[] finishStream() {
return compressBytes(new byte[0], 0, 0, false, true);
return compressBytes(new byte[0], 0, 0, true, true);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.meteogroup.jbrotli.BrotliError.DECOMPRESS_BROTLI_RESULT_NEEDS_MORE_OUTPUT;
import static org.meteogroup.jbrotli.BrotliErrorChecker.assertBrotliOk;

public final class BrotliStreamDeCompressor implements Closeable {
public final class BrotliStreamDeCompressor implements Closeable, AutoCloseable {

static {
assertBrotliOk(initJavaFieldIdCache());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ public void close() throws IOException {
public int getBrotliCompressionBufferSize() throws BrotliException {
return brotliStreamCompressor.getMaxInputBufferSize();
}

/**
* Write "finish" stream byte markers into the resulting stream.
* This is a mandatory step, to explicitly finish a brotli stream
* @throws IOException
* @see {@link BrotliStreamCompressor#finishStream()}
*/
public void finish() throws IOException {
outputStream.write(brotliStreamCompressor.finishStream());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,19 @@ public void tearDown() throws Exception {
@Test
public void compress_with_byte_array_and_flushing() throws Exception {
byte[] out = compressor.compressArray(A_BYTES, true);
// out = concat(out, compressor.finishStream());

assertThat(out).hasSize(10);
assertThat(out).isEqualTo(A_BYTES_COMPRESSED);
}

private byte[] concat(byte[] bytes1, byte[] bytes2) {
byte[] result = new byte[bytes1.length + bytes2.length];
System.arraycopy(bytes1, 0, result, 0, bytes1.length);
System.arraycopy(bytes2, 0, result, bytes1.length, bytes2.length);
return result;
}

@Test
public void compress_with_byte_array_without_flushing() throws Exception {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void byte_wise_compression_works() throws Exception {
for (int i : testBytes) {
brotliOutputStream.write(i);
}
brotliOutputStream.flush();
brotliOutputStream.finish();

// then
assertThat(decompress(baos.toByteArray(), testBytes.length)).isEqualTo(testBytes);
Expand All @@ -70,7 +70,7 @@ public void byte_array_wise_compression_works() throws Exception {

// when
brotliOutputStream.write(testBytes);
brotliOutputStream.flush();
brotliOutputStream.finish();

// then
assertThat(decompress(baos.toByteArray(), testBytes.length)).isEqualTo(testBytes);
Expand All @@ -85,7 +85,7 @@ public void big_byte_array_gets_compressed_when_underlying_brotli_compressor_is_

// when
brotliOutputStream.write(testBytes);
brotliOutputStream.flush();
brotliOutputStream.finish();

// then
assertThat(decompress(baos.toByteArray(), testBytes.length)).isEqualTo(testBytes);
Expand All @@ -96,7 +96,7 @@ public void byte_array_length_and_offset_wise_compression_works() throws Excepti

// when
brotliOutputStream.write(testBytes, 10, 100);
brotliOutputStream.flush();
brotliOutputStream.finish();

// then
byte[] decompressed = decompress(baos.toByteArray(), testBytes.length);
Expand Down

0 comments on commit 91e63c6

Please sign in to comment.