nixxcode/jvm-brotli

decompression error

Closed this issue · 1 comments

my code like this:
private static final int BUFF_SIZE = 4096;

private static void checkBrotliEnvironment() {
    if (!BrotliLoader.isBrotliAvailable()) {
        throw new RuntimeException("brotli not support, check about GNI environment");
    }
}

public static byte[] brotli(byte[] data) {
    checkBrotliEnvironment();
    return doBrotli(data, null);
}

private static byte[] doBrotli(byte[] data, @Nullable Parameters parameters) {
    try (ByteArrayOutputStream bos = new ByteArrayOutputStream(max(BUFF_SIZE, data.length / 2));
            BrotliOutputStream out = parameters == null ? new BrotliOutputStream(bos)
                                                        : new BrotliOutputStream(bos, parameters)) {
        out.write(data);
        out.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

public static byte[] unBrotli(byte[] data) {
    checkBrotliEnvironment();
    try (BrotliInputStream brotliInputStream = new BrotliInputStream(new ByteArrayInputStream(data))) {
        return toByteArray(brotliInputStream);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

private static byte[] toByteArray(InputStream input) throws IOException {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    copy(input, output, new byte[BUFF_SIZE]);
    return output.toByteArray();
}

private static long copy(InputStream input, OutputStream output, byte[] buffer)
        throws IOException {
    long count = 0;
    int n;
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
        count += n;
    }
    return count;
}

when i call brotli to compress a byte array , it seems working fine(no exception)
but error occurs when i call unBrotli (step 1 output as param).
message like this:

'java.lang.RuntimeException: java.io.IOException: unexpected end of input
Caused by: java.io.IOException: unexpected end of input
at com.nixxcode.jvmbrotli.dec.Decoder.fail(Decoder.java:50)
at com.nixxcode.jvmbrotli.dec.Decoder.decode(Decoder.java:90)
at com.nixxcode.jvmbrotli.dec.BrotliInputStream.read(BrotliInputStream.java:81)
at com.nixxcode.jvmbrotli.dec.BrotliInputStream.read(BrotliInputStream.java:73)'

What's wrong with the way I use it?Please help Me
@nixxcode

need call close of outputStream