From 3822e8b49ec69b4ab969572b6491144b56c603c5 Mon Sep 17 00:00:00 2001 From: Jeff Pinner Date: Sat, 21 Jan 2012 16:11:34 -0800 Subject: [PATCH] Fix #157: ZlibDecoder does not support preset dictionary --- .../handler/codec/compression/ZlibDecoder.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java b/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java index 1c89d42436bb..7904ef5b60db 100644 --- a/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java +++ b/codec/src/main/java/io/netty/handler/codec/compression/ZlibDecoder.java @@ -32,6 +32,7 @@ public class ZlibDecoder extends OneToOneDecoder { private final ZStream z = new ZStream(); + private byte[] dictionary; private volatile boolean finished; /** @@ -72,17 +73,13 @@ public ZlibDecoder(byte[] dictionary) { if (dictionary == null) { throw new NullPointerException("dictionary"); } + this.dictionary = dictionary; synchronized (z) { int resultCode; resultCode = z.inflateInit(JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); - } else { - resultCode = z.inflateSetDictionary(dictionary, dictionary.length); - if (resultCode != JZlib.Z_OK) { - ZlibUtil.fail(z, "failed to set the dictionary", resultCode); - } } } } @@ -131,6 +128,16 @@ protected Object decode(ChannelHandlerContext ctx, Channel channel, Object msg) z.next_out_index = 0; switch (resultCode) { + case JZlib.Z_NEED_DICT: + if (dictionary == null) { + ZlibUtil.fail(z, "decompression failure", resultCode); + } else { + resultCode = z.inflateSetDictionary(dictionary, dictionary.length); + if (resultCode != JZlib.Z_OK) { + ZlibUtil.fail(z, "failed to set the dictionary", resultCode); + } + } + break; case JZlib.Z_STREAM_END: finished = true; // Do not decode anymore. z.inflateEnd();