}
synchronized (z) {
try {
// Configure input.
ChannelBuffer compressed = (ChannelBuffer) msg;
byte[] in = new byte[compressed.readableBytes()];
compressed.readBytes(in);
z.next_in = in;
z.next_in_index = 0;
z.avail_in = in.length;
// Configure output.
byte[] out = new byte[in.length << 1];
ChannelBuffer decompressed = ChannelBuffers.dynamicBuffer(
compressed.order(), out.length,
ctx.getChannel().getConfig().getBufferFactory());
z.next_out = out;
z.next_out_index = 0;
z.avail_out = out.length;
loop: for (;;) {
// Decompress 'in' into 'out'
int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH);
if (z.next_out_index > 0) {
decompressed.writeBytes(out, 0, z.next_out_index);
z.avail_out = out.length;
}
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();
break loop;
case JZlib.Z_OK:
break;
case JZlib.Z_BUF_ERROR:
if (z.avail_in <= 0) {
break loop;
}
break;
default:
ZlibUtil.fail(z, "decompression failure", resultCode);
}
}
if (decompressed.writerIndex() != 0) { // readerIndex is always 0
return decompressed;
} else {
return null;
}
} finally {