nixxcode / jvm-brotli

Lightweight, cross-platform Java library for the Brotli compression format
72 stars 10 forks source link

"Unexpected end of input" #45

Open GintsJekabsons opened 1 week ago

GintsJekabsons commented 1 week ago

Hi!

I have found a file compression and decompression of which causes the following exception:

Exception in thread "main" 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:59)
    at Test.main(Test.java:120)

The file: test.txt

The code (I run it in Java 8):

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;

import org.apache.commons.io.output.ByteArrayOutputStream;

import com.nixxcode.jvmbrotli.common.BrotliLoader;
import com.nixxcode.jvmbrotli.dec.BrotliInputStream;
import com.nixxcode.jvmbrotli.enc.BrotliOutputStream;
import com.nixxcode.jvmbrotli.enc.Encoder;

public class Test {
    public static void main(String[] args) throws IOException {
        if (!BrotliLoader.isBrotliAvailable()) {
            System.out.println("Brotli not available");
            return;
        }
        byte[] bytes = Files.readAllBytes(new File("H:/Brotli/test.txt").toPath());

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BrotliOutputStream brotliOutputStream = new BrotliOutputStream(baos, new Encoder.Parameters().setQuality(2));
        brotliOutputStream.write(bytes);
        brotliOutputStream.close();

        InputStream is = new ByteArrayInputStream(baos.toByteArray());
        BrotliInputStream brotliInputStream = new BrotliInputStream(is);
        while (brotliInputStream.read() != -1) {}
        brotliInputStream.close();
    }
}

Expected behavior: the program does no output, just silently compresses and decompresses a file.

Actual behavior: the program raises the mentioned exception when the quality in setQuality() is set to at least 2. If lower, no exception.