[英]What's wrong with this use of java Deflater/Inflater with a dictionary
I'm trying to use Inflater
and Deflater
with a dictionary but it's not working. 我正在尝试将Inflater
和Deflater
与字典配合使用,但无法正常工作。 When I run this simple test program: 当我运行这个简单的测试程序时:
import java.io.*;
import java.util.*;
import java.util.zip.*;
public class DictTest {
public static void main(String[] args) throws Exception {
final int level = 9;
final boolean nowrap = true;
// compress
final Deflater def = new Deflater(level, nowrap);
final byte[] abcd = new byte[] { 0x41, 0x42, 0x43, 0x44 };
def.setDictionary(abcd);
def.setInput(abcd);
def.finish();
final byte[] buf = new byte[1024];
final int nbytes = def.deflate(buf);
assert def.finished();
def.end();
// decompress
final Inflater inf = new Inflater(nowrap);
inf.setInput(buf, 0, nbytes + 1); // include extra "dummy" byte
while (true) {
while (inf.inflate(buf) != 0) {
// discard
}
assert !inf.needsInput();
if (inf.finished())
break;
assert inf.needsDictionary();
inf.setDictionary(abcd);
continue;
}
inf.end();
}
}
I get this exception: 我得到这个例外:
$ javac DictTest.java && java -ea DictTest
Exception in thread "main" java.util.zip.DataFormatException: invalid distance too far back
at java.util.zip.Inflater.inflateBytes(Native Method)
at java.util.zip.Inflater.inflate(Inflater.java:259)
at java.util.zip.Inflater.inflate(Inflater.java:280)
at DictTest.main(DictTest.java:27)
What am I doing wrong? 我究竟做错了什么? Thanks. 谢谢。
Set the inflater dictionary before you set input. 在设置输入之前,请设置充气机字典。 Also, your infinite loop will run (forever). 另外,您的无限循环将运行(永远)。 You wanted something like 你想要类似的东西
final int level = 9;
final boolean nowrap = true;
// compress
final Deflater def = new Deflater(level, nowrap);
final byte[] abcd = new byte[] { 0x41, 0x42, 0x43, 0x44 };
def.setDictionary(abcd);
def.setInput(abcd);
def.finish();
final byte[] buf = new byte[1024];
final int nbytes = def.deflate(buf);
assert def.finished();
def.end();
// decompress
final Inflater inf = new Inflater(nowrap);
inf.setDictionary(abcd);
inf.setInput(buf); // include extra "dummy" byte
while (inf.inflate(buf) != 0) {
// discard
}
assert !inf.needsInput();
assert inf.needsDictionary();
inf.end();
And it then runs without error here. 然后它在这里运行而没有错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.