How to decompress lzo byte array using java-lzo library?

元气小坏坏 提交于 2019-12-11 08:48:50

问题


I'm trying to decompress compressed byte array using java-lzo library. I'm following this reference.

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)

So, while initializing LzoInputStream I'm wrong or after that I need to do something? Any suggestions will be appreciated!


回答1:


Please ensure that lzo stream are are flushed and closed during compression.



来源:https://stackoverflow.com/questions/45398848/how-to-decompress-lzo-byte-array-using-java-lzo-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!