I am seeing an EOFException exception while reading a SQLite
file from temp directory. Following is the code for reading the file. And also the exception is not seen always. Consider out of 50K files it is coming for 3 to 4 times.
public static byte[] decompressLzmaStream(InputStream inputStream, int size)
throws CompressorException, IOException {
if(size < 1) {
size = 1024 * 100;
}
try(LZMACompressorInputStream lzmaInputStream =
new LZMACompressorInputStream(inputStream);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(size)) {
byte[] buffer = new byte[size];
int length;
while (-1 != (length = lzmaInputStream.read(buffer))) {
byteArrayOutputStream.write(buffer, 0, length);
}
byteArrayOutputStream.flush();
return byteArrayOutputStream.toByteArray();
}
}
I am using the following dependency for the decompression
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.20</version>
</dependency>
The exception is thrown at while (-1 != (length = lzmaInputStream.read(buffer))) {
line. Following is the exception.
java.io.EOFException: null at java.io.DataInputStream.readUnsignedByte(DataInputStream.java:290)
at org.tukaani.xz.rangecoder.RangeDecoderFromStream.normalize(Unknown Source)
at org.tukaani.xz.rangecoder.RangeDecoder.decodeBit(Unknown Source)
at org.tukaani.xz.lzma.LZMADecoder.decode(Unknown Source)
at org.tukaani.xz.LZMAInputStream.read(Unknown Source)
at org.apache.commons.compress.compressors.lzma.
LZMACompressorInputStream.read(LZMACompressorInputStream.java:62)
at java.io.InputStream.read(InputStream.java:101)
Anyone have any idea about the following constructor of commons-compress
.
// I am using this constructor of LZMACompressorInputStream
public LZMACompressorInputStream(InputStream inputStream) throws IOException {
this.in = new LZMAInputStream(this.countingStream = new CountingInputStream(inputStream), -1);
}
// This is added in later version of commons-compress, what is memoryLimitInKb
public LZMACompressorInputStream(InputStream inputStream, int memoryLimitInKb) throws IOException {
try {
this.in = new LZMAInputStream(this.countingStream = new CountingInputStream(inputStream), memoryLimitInKb);
} catch (MemoryLimitException var4) {
throw new org.apache.commons.compress.MemoryLimitException((long)var4.getMemoryNeeded(), var4.getMemoryLimit(), var4);
}
}
As I read for LZMA streams we need to pass the uncompressed size to the constructor here --> https://issues.apache.org/jira/browse/COMPRESS-286?page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel&focusedCommentId=14109417#comment-14109417