jzlib 1.0.7: ArrayOutOfBoundException in ZOutputStream.write()
I have a simple test case producing a sure ArrayOutOfBoundException in jzlib 1.0.7 depending on the data subsequently written to one and the same instance of ZOutputStream.
Stacktrace:
java.lang.ArrayIndexOutOfBoundsException: 587
at com.jcraft.jzlib.Tree.d_code(Tree.java:149)
at com.jcraft.jzlib.Deflate.compress_block(Deflate.java:691)
at com.jcraft.jzlib.Deflate._tr_flush_block(Deflate.java:897)
at com.jcraft.jzlib.Deflate.flush_block_only(Deflate.java:772)
at com.jcraft.jzlib.Deflate.deflate_slow(Deflate.java:1195)
at com.jcraft.jzlib.Deflate.deflate(Deflate.java:1567)
at com.jcraft.jzlib.ZStream.deflate(ZStream.java:133)
at com.jcraft.jzlib.ZOutputStream.write(ZOutputStream.java:102)
at com.jcraft.jzlib.JZLibTestCase.main(JZLibTestCase.java:51)
at JZLibTestCase.main(JZLibTestCase.java:28)
The problem occurs very rarely and depends on the data subsequentially written to an open ZOutputStream from jzlib.
Do you have a hint how to fix this? Have you ever heard of this?
Re: jzlib 1.0.7: ArrayOutOfBoundException in ZOutputStream.write()
Of course, but the attachment is necessary to reproduce it, absolutely. You most probable won't be able to reproduce it with own data to be compressed. We got this problem after years running jzlib successfully in productive environments. The test data is imported from files, not in the code itself.
Here is the code for importing:
Code:
import java.io.*;
import java.util.*;
import com.jcraft.jzlib.*;
public class JZLibTestCase {
public static void main(String[] args)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputStream zout;
try
{
List<File> files = getFileListing(new File("dump"));
// Even Z_DEFAULT_COMPRESSION does not help
zout = new ZOutputStream(bos, JZlib.Z_BEST_COMPRESSION);
// Commenting this in, there is no error any longer
//zout = new ZOutputStream(bos, JZlib.Z_NO_COMPRESSION);
// Writing only contents of the last file on the list does not help
// The bug depends on the internal state of this ZOutputStream instance.
//File file = new File("/home/rkrell/test/jzlib/com.jcraft.jzlib.ZOutputStream@38a97b0b_1302538064113_0_8192");
for(File file : files ){
System.out.println("File: " + file.getName());
byte[] b = getBytesFromFile(file);
zout.write(b, 0, b.length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static private List<File> getFileListing(File dir)
throws FileNotFoundException
{
List<File> result = getFileListingNoSort(dir);
Collections.sort(result);
return result;
}
static private List<File> getFileListingNoSort(File dir)
throws FileNotFoundException
{
List<File> result = new ArrayList<File>();
File[] filesAndDirs = dir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for(File file : filesDirs) {
if ( file.isFile() ) {
result.add(file);
}
}
return result;
}
static private byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file);
long length = file.length();
byte[] bytes = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < bytes.length
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
offset += numRead;
}
if (offset < bytes.length) {
throw new IOException("Could not completely read file "+file.getName());
}
is.close();
return bytes;
}
}
Re: jzlib 1.0.7: ArrayOutOfBoundException in ZOutputStream.write()
If it seems to be a problem in the jzLib library code, report it to JCraft.
Successful software always gets changed...
F. Brooks
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Re: jzlib 1.0.7: ArrayOutOfBoundException in ZOutputStream.write()
I know this, and it is already reported to JCraft. But it is open source, I ask here, whether this has one already solved. I haven't got any echo from JCraft to this time.
Switching this test case to java.util.zip and GZOutputStream works.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.