问题
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