Skip to content

Commit

Permalink
Fix netty#157: ZlibDecoder does not support preset dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Pinner committed Jan 22, 2012
1 parent b96768c commit 3822e8b
Showing 1 changed file with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
public class ZlibDecoder extends OneToOneDecoder {

private final ZStream z = new ZStream();
private byte[] dictionary;
private volatile boolean finished;

/**
Expand Down Expand Up @@ -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);
}
}
}
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 3822e8b

Please sign in to comment.