lzo-java icon indicating copy to clipboard operation
lzo-java copied to clipboard

How to solve java.io.EOFException?

Open ketan5452 opened this issue 7 years ago • 2 comments

I'm trying to decompress compressed byte array using java-lzo library. I added below maven dependency to pom.xml -

 <dependency>
        <groupId>org.anarres.lzo</groupId>
        <artifactId>lzo-core</artifactId>
       <version>1.0.5</version>
</dependency>

I created one method which accepts lzo compressed byte array and destination byte array length as a argument.

Program :

private byte[] decompress(byte[] src, int len) {
    ByteArrayInputStream input = new ByteArrayInputStream(src);
    ByteArrayOutputStream out = new ByteArrayOutputStream();

    LzoAlgorithm algorithm = LzoAlgorithm.LZO1X;
    lzo_uintp lzo = new lzo_uintp(len);
    LzoDecompressor decompressor = LzoLibrary.getInstance().newDecompressor(algorithm, null);
    LzoInputStream stream = new LzoInputStream(input, decompressor);

    try {
        int data = stream.read();
        while (data != -1) {
            out.write(data);
            data = stream.read();
        }
        out.flush();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return out.toByteArray();
}

I got stuck at one point because stream.read() always returns a "-1". I checked input array it is filled with data. Further I checked using stream.available() method but this method also returns always "0" in my case. But If I checked to InputStream like input.available() then the return value is length of array.

Error is same just like I said it is returning "-1" -

java.io.EOFException at org.anarres.lzo.LzoInputStream.readBytes(LzoInputStream.java:183) at org.anarres.lzo.LzoInputStream.readBlock(LzoInputStream.java:132) at org.anarres.lzo.LzoInputStream.fill(LzoInputStream.java:119) at org.anarres.lzo.LzoInputStream.read(LzoInputStream.java:90)

What's wrong with this code?

ketan5452 avatar Jul 31 '17 18:07 ketan5452

I'm going to guess, since LZO is essentially a prefix/length encoding, that there's a byte in your incoming compressed bytestream which specifies a length longer than the remaining data. i.e. you're reading something which says [literal length=24][only 22 bytes] so the EOFException says 2 bytes are missing.

That's a wild guess.

shevek avatar Aug 18 '17 01:08 shevek

get the same error

user20161119 avatar Jul 15 '19 09:07 user20161119