次のコードは、java.util.zip.Deflaterのjavadocsに記載されている例に基づいています。私が行った唯一の変更は、dictというバイト配列を作成し、setDictionary(byte [])メソッドを使用してDeflaterインスタンスとInflaterインスタンスの両方にディクショナリを設定することです。
私が見ている問題は、Deflaterに使用したものとまったく同じ配列でInflater.setDictionary()を呼び出すと、IllegalArgumentExceptionが発生することです。
問題のコードは次のとおりです。
import java.util.zip.Deflater;
import java.util.zip.Inflater;
public class DeflateWithDictionary {
public static void main(String[] args) throws Exception {
String inputString = "blahblahblahblahblah??";
byte[] input = inputString.getBytes("UTF-8");
byte[] dict = "blah".getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater();
compresser.setInput(input);
compresser.setDictionary(dict);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
// Decompress the bytes
Inflater decompresser = new Inflater();
decompresser.setInput(output, 0, compressedDataLength);
decompresser.setDictionary(dict); //IllegalArgumentExeption thrown here
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
System.out.println("Decompressed String: " + outputString);
}
}
辞書を設定せずに同じ圧縮バイトをデフレートしようとすると、エラーは発生しませんが、返される結果はゼロバイトです。
Deflater / Inflaterでカスタム辞書を使用するために特別なことはありますか?