@Override
 public synchronized void end() {
   if (finished) {
     return;
   }
   finished = true;
   z.deflateEnd();
   z.next_in = null;
   z.next_out = null;
 }
  private void encode(ChannelBuffer compressed) {
    try {
      byte[] out = new byte[(int) Math.ceil(z.next_in.length * 1.001) + 12];
      z.next_out = out;
      z.next_out_index = 0;
      z.avail_out = out.length;

      int resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
      if (resultCode != JZlib.Z_OK) {
        throw new CompressionException("compression failure: " + resultCode);
      }

      if (z.next_out_index != 0) {
        compressed.writeBytes(out, 0, z.next_out_index);
      }
    } finally {
      // Deference the external references explicitly to tell the VM that
      // the allocated byte arrays are temporary so that the call stack
      // can be utilized.
      // I'm not sure if the modern VMs do this optimization though.
      z.next_in = null;
      z.next_out = null;
    }
  }