Java Code Examples for com.jcraft.jzlib.JZlib#Z_OK
The following examples show how to use
com.jcraft.jzlib.JZlib#Z_OK .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ZLibCompression.java From j2ssh-maverick with GNU Lesser General Public License v3.0 | 6 votes |
public byte[] compress(byte[] buf, int start, int len) throws IOException { compressOut.reset(); stream.next_in = buf; stream.next_in_index = start; stream.avail_in = len - start; int status; do { stream.next_out = tmpbuf; stream.next_out_index = 0; stream.avail_out = BUF_SIZE; status = stream.deflate(JZlib.Z_PARTIAL_FLUSH); switch (status) { case JZlib.Z_OK: compressOut.write(tmpbuf, 0, BUF_SIZE - stream.avail_out); break; default: throw new IOException("compress: deflate returnd " + status); } } while (stream.avail_out == 0); return compressOut.toByteArray(); }
Example 2
Source File: SpdyHeaderBlockJZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
SpdyHeaderBlockJZlibEncoder( SpdyVersion version, int compressionLevel, int windowBits, int memLevel) { super(version); if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (windowBits < 9 || windowBits > 15) { throw new IllegalArgumentException( "windowBits: " + windowBits + " (expected: 9-15)"); } if (memLevel < 1 || memLevel > 9) { throw new IllegalArgumentException( "memLevel: " + memLevel + " (expected: 1-9)"); } int resultCode = z.deflateInit( compressionLevel, windowBits, memLevel, JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { throw new CompressionException( "failed to initialize an SPDY header block deflater: " + resultCode); } else { resultCode = z.deflateSetDictionary(SPDY_DICT, SPDY_DICT.length); if (resultCode != JZlib.Z_OK) { throw new CompressionException( "failed to set the SPDY dictionary: " + resultCode); } } }
Example 3
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Deflate (compress) the passed-in bytes and -- if appropriate -- * send the compressed bytes downstream to the filter's output stream.<p> * * This write method does not necessarily cause a write to the * server -- a write will only occur when the jzBytes buffer * is full, or on a later flush. This is a consequence of the * way GZIP streaming works here, and means you must ensure that * a suitable flush is done at a suitable (packet) boundary. See * the comments for flush() below. * * @see java.io.FilterOutputStream#write(byte[], int, int) */ @Override public void write(byte[] bytes, int offset, int len) throws IOException { if (bytes == null) { throw new NullPointerError( "null byte array passed to RpcGZIPOutputStream.write()"); } if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) { throw new P4JavaError( "bad length or offset in RpcGZIPOutputStream.write()"); } this.jzOutputSream.next_in = bytes; this.jzOutputSream.avail_in = len; this.jzOutputSream.next_in_index = offset; while (this.jzOutputSream.avail_in != 0) { if (this.jzOutputSream.avail_out == 0) { this.out.write(this.jzBytes); this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe... this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("connection compression error: " + getJZlibErrorStr(jzErr)); } } }
Example 4
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Flush the results of previous byte deflation (compression) downstream.<p> * * As a consequence of the way GZIP streaming works, this flush is often the only * place where bytes are actually written downstream towards the server (the earlier * writes may only write to the internal buffer here). Using flush causes a compression * boundary, so it should only be used after a complete packet has been put onto * this stream -- i.e. users of this stream must call flush appropriately, or the * server may not see packets at all. * * @see java.io.FilterOutputStream#flush() */ @Override public void flush() throws IOException { this.jzOutputSream.avail_in = 0; boolean done = false; while (true) { if ((this.jzOutputSream.avail_out == 0) || done) { out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out); this.jzOutputSream.next_out = this.jzBytes; this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } if (done) { break; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("Perforce connection compression error: " + getJZlibErrorStr(jzErr)); } if (this.jzOutputSream.avail_out != 0) { done = true; } } }
Example 5
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Deflate (compress) the passed-in bytes and -- if appropriate -- * send the compressed bytes downstream to the filter's output stream.<p> * * This write method does not necessarily cause a write to the * server -- a write will only occur when the jzBytes buffer * is full, or on a later flush. This is a consequence of the * way GZIP streaming works here, and means you must ensure that * a suitable flush is done at a suitable (packet) boundary. See * the comments for flush() below. * * @see java.io.FilterOutputStream#write(byte[], int, int) */ @Override public void write(byte[] bytes, int offset, int len) throws IOException { if (bytes == null) { throw new NullPointerError( "null byte array passed to RpcGZIPOutputStream.write()"); } if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) { throw new P4JavaError( "bad length or offset in RpcGZIPOutputStream.write()"); } this.jzOutputSream.next_in = bytes; this.jzOutputSream.avail_in = len; this.jzOutputSream.next_in_index = offset; while (this.jzOutputSream.avail_in != 0) { if (this.jzOutputSream.avail_out == 0) { this.out.write(this.jzBytes); this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe... this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("connection compression error: " + getJZlibErrorStr(jzErr)); } } }
Example 6
Source File: JZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * Creates a new zlib encoder with the specified {@code compressionLevel}, * the specified {@code windowBits}, the specified {@code memLevel}, * and the specified preset dictionary. The wrapper is always * {@link ZlibWrapper#ZLIB} because it is the only format that supports * the preset dictionary. * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. * @param windowBits * The base two logarithm of the size of the history buffer. The * value should be in the range {@code 9} to {@code 15} inclusive. * Larger values result in better compression at the expense of * memory usage. The default value is {@code 15}. * @param memLevel * How much memory should be allocated for the internal compression * state. {@code 1} uses minimum memory and {@code 9} uses maximum * memory. Larger values result in better and faster compression * at the expense of memory usage. The default value is {@code 8} * @param dictionary the preset dictionary * * @throws CompressionException if failed to initialize zlib */ public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (windowBits < 9 || windowBits > 15) { throw new IllegalArgumentException( "windowBits: " + windowBits + " (expected: 9-15)"); } if (memLevel < 1 || memLevel > 9) { throw new IllegalArgumentException( "memLevel: " + memLevel + " (expected: 1-9)"); } if (dictionary == null) { throw new NullPointerException("dictionary"); } int resultCode; resultCode = z.deflateInit( compressionLevel, windowBits, memLevel, JZlib.W_ZLIB); // Default: ZLIB format if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } else { resultCode = z.deflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode); } } wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB); }
Example 7
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Flush the results of previous byte deflation (compression) downstream.<p> * * As a consequence of the way GZIP streaming works, this flush is often the only * place where bytes are actually written downstream towards the server (the earlier * writes may only write to the internal buffer here). Using flush causes a compression * boundary, so it should only be used after a complete packet has been put onto * this stream -- i.e. users of this stream must call flush appropriately, or the * server may not see packets at all. * * @see java.io.FilterOutputStream#flush() */ @Override public void flush() throws IOException { this.jzOutputSream.avail_in = 0; boolean done = false; while (true) { if ((this.jzOutputSream.avail_out == 0) || done) { out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out); this.jzOutputSream.next_out = this.jzBytes; this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } if (done) { break; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("Perforce connection compression error: " + getJZlibErrorStr(jzErr)); } if (this.jzOutputSream.avail_out != 0) { done = true; } } }
Example 8
Source File: JZlibDecoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
/** * Creates a new instance with the specified preset dictionary. The wrapper * is always {@link ZlibWrapper#ZLIB} because it is the only format that * supports the preset dictionary. * * @throws DecompressionException if failed to initialize zlib */ public JZlibDecoder(byte[] dictionary) { if (dictionary == null) { throw new NullPointerException("dictionary"); } this.dictionary = dictionary; int resultCode; resultCode = z.inflateInit(JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } }
Example 9
Source File: ZLibCompression.java From j2ssh-maverick with GNU Lesser General Public License v3.0 | 5 votes |
public byte[] uncompress(byte[] buffer, int start, int length) throws IOException { // int inflated_end = 0; uncompressOut.reset(); stream.next_in = buffer; stream.next_in_index = start; stream.avail_in = length; while (true) { stream.next_out = inflated_buf; stream.next_out_index = 0; stream.avail_out = BUF_SIZE; int status = stream.inflate(JZlib.Z_PARTIAL_FLUSH); switch (status) { case JZlib.Z_OK: uncompressOut.write(inflated_buf, 0, BUF_SIZE - stream.avail_out); break; case JZlib.Z_BUF_ERROR: return uncompressOut.toByteArray(); default: throw new IOException("uncompress: inflate returnd " + status); } } }
Example 10
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Deflate (compress) the passed-in bytes and -- if appropriate -- * send the compressed bytes downstream to the filter's output stream.<p> * * This write method does not necessarily cause a write to the * server -- a write will only occur when the jzBytes buffer * is full, or on a later flush. This is a consequence of the * way GZIP streaming works here, and means you must ensure that * a suitable flush is done at a suitable (packet) boundary. See * the comments for flush() below. * * @see java.io.FilterOutputStream#write(byte[], int, int) */ @Override public void write(byte[] bytes, int offset, int len) throws IOException { if (bytes == null) { throw new NullPointerError( "null byte array passed to RpcGZIPOutputStream.write()"); } if ((len <= 0) || (offset < 0) || (offset >= bytes.length) || (len > (bytes.length - offset))) { throw new P4JavaError( "bad length or offset in RpcGZIPOutputStream.write()"); } this.jzOutputSream.next_in = bytes; this.jzOutputSream.avail_in = len; this.jzOutputSream.next_in_index = offset; while (this.jzOutputSream.avail_in != 0) { if (this.jzOutputSream.avail_out == 0) { this.out.write(this.jzBytes); this.jzOutputSream.next_out = this.jzBytes; // redundant, but safe... this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_NO_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("connection compression error: " + getJZlibErrorStr(jzErr)); } } }
Example 11
Source File: ITCHDeflateCodec.java From sailfish-core with Apache License 2.0 | 5 votes |
private void CHECK_ERR(ZStream z, int err, String msg) { if (err != JZlib.Z_OK) { if(z.msg != null) { logger.error(z.msg); } logger.error("{} error: {}", msg, err); throw new RuntimeException("Error on decode: " + msg + " error: " + err); } }
Example 12
Source File: JZlibEncoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Creates a new zlib encoder with the specified {@code compressionLevel}, * the specified {@code windowBits}, the specified {@code memLevel}, * and the specified preset dictionary. The wrapper is always * {@link ZlibWrapper#ZLIB} because it is the only format that supports * the preset dictionary. * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. * @param windowBits * The base two logarithm of the size of the history buffer. The * value should be in the range {@code 9} to {@code 15} inclusive. * Larger values result in better compression at the expense of * memory usage. The default value is {@code 15}. * @param memLevel * How much memory should be allocated for the internal compression * state. {@code 1} uses minimum memory and {@code 9} uses maximum * memory. Larger values result in better and faster compression * at the expense of memory usage. The default value is {@code 8} * @param dictionary the preset dictionary * * @throws CompressionException if failed to initialize zlib * 使用指定的压缩级别、指定的windowBits、指定的memLevel和指定的预设置字典创建一个新的zlib编码器。包装器总是ZlibWrapper。ZLIB是因为它是唯一支持预设字典的格式。 */ public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (windowBits < 9 || windowBits > 15) { throw new IllegalArgumentException( "windowBits: " + windowBits + " (expected: 9-15)"); } if (memLevel < 1 || memLevel > 9) { throw new IllegalArgumentException( "memLevel: " + memLevel + " (expected: 1-9)"); } if (dictionary == null) { throw new NullPointerException("dictionary"); } int resultCode; resultCode = z.deflateInit( compressionLevel, windowBits, memLevel, JZlib.W_ZLIB); // Default: ZLIB format if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } else { resultCode = z.deflateSetDictionary(dictionary, dictionary.length); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "failed to set the dictionary", resultCode); } } wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB); }
Example 13
Source File: JZlibEncoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Creates a new zlib encoder with the specified {@code compressionLevel}, * the specified {@code windowBits}, the specified {@code memLevel}, and * the specified wrapper. * * @param compressionLevel * {@code 1} yields the fastest compression and {@code 9} yields the * best compression. {@code 0} means no compression. The default * compression level is {@code 6}. * @param windowBits * The base two logarithm of the size of the history buffer. The * value should be in the range {@code 9} to {@code 15} inclusive. * Larger values result in better compression at the expense of * memory usage. The default value is {@code 15}. * @param memLevel * How much memory should be allocated for the internal compression * state. {@code 1} uses minimum memory and {@code 9} uses maximum * memory. Larger values result in better and faster compression * at the expense of memory usage. The default value is {@code 8} * * @throws CompressionException if failed to initialize zlib * 使用指定的压缩级别、指定的windowbit、指定的memLevel和指定的包装器创建新的zlib编码器。 */ public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) { if (compressionLevel < 0 || compressionLevel > 9) { throw new IllegalArgumentException( "compressionLevel: " + compressionLevel + " (expected: 0-9)"); } if (windowBits < 9 || windowBits > 15) { throw new IllegalArgumentException( "windowBits: " + windowBits + " (expected: 9-15)"); } if (memLevel < 1 || memLevel > 9) { throw new IllegalArgumentException( "memLevel: " + memLevel + " (expected: 1-9)"); } if (wrapper == null) { throw new NullPointerException("wrapper"); } if (wrapper == ZlibWrapper.ZLIB_OR_NONE) { throw new IllegalArgumentException( "wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " + "allowed for compression."); } int resultCode = z.init( compressionLevel, windowBits, memLevel, ZlibUtil.convertWrapperType(wrapper)); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } wrapperOverhead = ZlibUtil.wrapperOverhead(wrapper); }
Example 14
Source File: JZlibDecoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
/** * Creates a new instance with the specified preset dictionary. The wrapper * is always {@link ZlibWrapper#ZLIB} because it is the only format that * supports the preset dictionary.使用指定的预置字典创建一个新实例。包装器总是ZlibWrapper。ZLIB是因为它是唯一支持预设字典的格式。 * * @throws DecompressionException if failed to initialize zlib */ public JZlibDecoder(byte[] dictionary) { if (dictionary == null) { throw new NullPointerException("dictionary"); } this.dictionary = dictionary; int resultCode; resultCode = z.inflateInit(JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { ZlibUtil.fail(z, "initialization failure", resultCode); } }
Example 15
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Flush the results of previous byte deflation (compression) downstream.<p> * * As a consequence of the way GZIP streaming works, this flush is often the only * place where bytes are actually written downstream towards the server (the earlier * writes may only write to the internal buffer here). Using flush causes a compression * boundary, so it should only be used after a complete packet has been put onto * this stream -- i.e. users of this stream must call flush appropriately, or the * server may not see packets at all. * * @see java.io.FilterOutputStream#flush() */ @Override public void flush() throws IOException { this.jzOutputSream.avail_in = 0; boolean done = false; while (true) { if ((this.jzOutputSream.avail_out == 0) || done) { out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out); this.jzOutputSream.next_out = this.jzBytes; this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } if (done) { break; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("Perforce connection compression error: " + getJZlibErrorStr(jzErr)); } if (this.jzOutputSream.avail_out != 0) { done = true; } } }
Example 16
Source File: RpcGZIPOutputStream.java From p4ic4idea with Apache License 2.0 | 5 votes |
/** * Flush the results of previous byte deflation (compression) downstream.<p> * * As a consequence of the way GZIP streaming works, this flush is often the only * place where bytes are actually written downstream towards the server (the earlier * writes may only write to the internal buffer here). Using flush causes a compression * boundary, so it should only be used after a complete packet has been put onto * this stream -- i.e. users of this stream must call flush appropriately, or the * server may not see packets at all. * * @see java.io.FilterOutputStream#flush() */ @Override public void flush() throws IOException { this.jzOutputSream.avail_in = 0; boolean done = false; while (true) { if ((this.jzOutputSream.avail_out == 0) || done) { out.write(this.jzBytes, 0, this.jzBytes.length - this.jzOutputSream.avail_out); this.jzOutputSream.next_out = this.jzBytes; this.jzOutputSream.avail_out = this.jzBytes.length; this.jzOutputSream.next_out_index = 0; } if (done) { break; } int jzErr = this.jzOutputSream.deflate(JZlib.Z_FULL_FLUSH); if (jzErr != JZlib.Z_OK) { throw new IOException("Perforce connection compression error: " + getJZlibErrorStr(jzErr)); } if (this.jzOutputSream.avail_out != 0) { done = true; } } }
Example 17
Source File: Zlib.java From neoscada with Eclipse Public License 1.0 | 4 votes |
/** * Uncompress the given buffer, returning it in a new buffer. * * @param inBuffer the {@link IoBuffer} to be decompressed. The contents * of the buffer are transferred into a local byte array and the buffer is * flipped and returned intact. * @return the decompressed data * @throws IOException if the decompression of the data failed for some reason. * @throws IllegalArgumentException if the mode is not <code>MODE_DEFLATER</code> */ public IoBuffer inflate(IoBuffer inBuffer) throws IOException { if (mode == MODE_DEFLATER) { throw new IllegalStateException("not initialized as INFLATER"); } byte[] inBytes = new byte[inBuffer.remaining()]; inBuffer.get(inBytes).flip(); // We could probably do this better, if we're willing to return multiple buffers // (e.g. with a callback function) byte[] outBytes = new byte[inBytes.length * 2]; IoBuffer outBuffer = IoBuffer.allocate(outBytes.length); outBuffer.setAutoExpand(true); synchronized (zStream) { zStream.next_in = inBytes; zStream.next_in_index = 0; zStream.avail_in = inBytes.length; zStream.next_out = outBytes; zStream.next_out_index = 0; zStream.avail_out = outBytes.length; int retval = 0; do { retval = zStream.inflate(JZlib.Z_SYNC_FLUSH); switch (retval) { case JZlib.Z_OK: // completed decompression, lets copy data and get out case JZlib.Z_BUF_ERROR: // need more space for output. store current output and get more outBuffer.put(outBytes, 0, zStream.next_out_index); zStream.next_out_index = 0; zStream.avail_out = outBytes.length; break; default: // unknown error outBuffer = null; if (zStream.msg == null) { throw new IOException("Unknown error. Error code : " + retval); } else { throw new IOException("Unknown error. Error code : " + retval + " and message : " + zStream.msg); } } } while (zStream.avail_in > 0); } return outBuffer.flip(); }
Example 18
Source File: SpdyHeaderBlockJZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
private ByteBuf encode(ByteBufAllocator alloc) { boolean release = true; ByteBuf out = null; try { int oldNextInIndex = z.next_in_index; int oldNextOutIndex = z.next_out_index; int maxOutputLength = (int) Math.ceil(z.next_in.length * 1.001) + 12; out = alloc.heapBuffer(maxOutputLength); z.next_out = out.array(); z.next_out_index = out.arrayOffset() + out.writerIndex(); z.avail_out = maxOutputLength; int resultCode; try { resultCode = z.deflate(JZlib.Z_SYNC_FLUSH); } finally { out.skipBytes(z.next_in_index - oldNextInIndex); } if (resultCode != JZlib.Z_OK) { throw new CompressionException("compression failure: " + resultCode); } int outputLength = z.next_out_index - oldNextOutIndex; if (outputLength > 0) { out.writerIndex(out.writerIndex() + outputLength); } release = false; return out; } 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; if (release && out != null) { out.release(); } } }
Example 19
Source File: JZlibEncoder.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; ByteBuf footer; try { // Configure input. z.next_in = EmptyArrays.EMPTY_BYTES; z.next_in_index = 0; z.avail_in = 0; // Configure output. byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header z.next_out = out; z.next_out_index = 0; z.avail_out = out.length; // Write the ADLER32 checksum (stream footer). int resultCode = z.deflate(JZlib.Z_FINISH); if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) { promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode)); return promise; } else if (z.next_out_index != 0) { footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index); } else { footer = Unpooled.EMPTY_BUFFER; } } finally { z.deflateEnd(); // 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; } return ctx.writeAndFlush(footer, promise); }
Example 20
Source File: JZlibEncoder.java From netty4.0.27Learn with Apache License 2.0 | 4 votes |
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; ByteBuf footer; try { // Configure input. z.next_in = EmptyArrays.EMPTY_BYTES; z.next_in_index = 0; z.avail_in = 0; // Configure output. byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header z.next_out = out; z.next_out_index = 0; z.avail_out = out.length; // Write the ADLER32 checksum (stream footer). int resultCode = z.deflate(JZlib.Z_FINISH); if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) { promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode)); return promise; } else if (z.next_out_index != 0) { footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index); } else { footer = Unpooled.EMPTY_BUFFER; } } finally { z.deflateEnd(); // 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; } return ctx.writeAndFlush(footer, promise); }