public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) { Contract.Requires(compressionLevel >= 0 && compressionLevel <= 9); Contract.Requires(windowBits >= 9 && windowBits <= 15); Contract.Requires(memLevel >= 1 && memLevel <= 9); int resultCode = this.z.DeflateInit( compressionLevel, windowBits, memLevel, JZlib.W_ZLIB); // Default: ZLIB format if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "initialization failure", resultCode); } else { resultCode = this.z.DeflateSetDictionary(dictionary, dictionary.Length); if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "failed to set the dictionary", resultCode); } } this.wrapperOverhead = ZlibUtil.WrapperOverhead(ZlibWrapper.Zlib); }
public JZlibDecoder(ZlibWrapper wrapper) { int resultCode = this.z.Init(ZlibUtil.ConvertWrapperType(wrapper)); if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "initialization failure", resultCode); } }
public JZlibDecoder(byte[] dictionary) { Contract.Requires(dictionary != null); this.dictionary = dictionary; int resultCode; resultCode = this.z.InflateInit(JZlib.W_ZLIB); if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "initialization failure", resultCode); } }
/** * 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 */ public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) { Contract.Requires(compressionLevel >= 0 && compressionLevel <= 9); Contract.Requires(windowBits >= 9 && windowBits <= 15); Contract.Requires(memLevel >= 1 && memLevel <= 9); int resultCode = this.z.Init( compressionLevel, windowBits, memLevel, ZlibUtil.ConvertWrapperType(wrapper)); if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "initialization failure", resultCode); } this.wrapperOverhead = ZlibUtil.WrapperOverhead(wrapper); }
protected override void Encode(IChannelHandlerContext context, IByteBuffer message, IByteBuffer output) { if (this.finished) { output.WriteBytes(message); return; } int inputLength = message.ReadableBytes; if (inputLength == 0) { return; } try { // Configure input. bool inHasArray = message.HasArray; this.z.avail_in = inputLength; if (inHasArray) { this.z.next_in = message.Array; this.z.next_in_index = message.ArrayOffset + message.ReaderIndex; } else { var array = new byte[inputLength]; message.GetBytes(message.ReaderIndex, array); this.z.next_in = array; this.z.next_in_index = 0; } int oldNextInIndex = this.z.next_in_index; // Configure output. int maxOutputLength = (int)Math.Ceiling(inputLength * 1.001) + 12 + this.wrapperOverhead; output.EnsureWritable(maxOutputLength); this.z.avail_out = maxOutputLength; this.z.next_out = output.Array; this.z.next_out_index = output.ArrayOffset + output.WriterIndex; int oldNextOutIndex = this.z.next_out_index; int resultCode; try { resultCode = this.z.Deflate(JZlib.Z_SYNC_FLUSH); } finally { message.SkipBytes(this.z.next_in_index - oldNextInIndex); } if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "compression failure", resultCode); } int outputLength = this.z.next_out_index - oldNextOutIndex; if (outputLength > 0) { output.SetWriterIndex(output.WriterIndex + outputLength); } } finally { this.z.next_in = null; this.z.next_out = null; } }
protected internal override void Decode(IChannelHandlerContext context, IByteBuffer input, List <object> output) { if (this.finished) { // Skip data received after finished. input.SkipBytes(input.ReadableBytes); return; } int inputLength = input.ReadableBytes; if (inputLength == 0) { return; } try { // Configure input. this.z.avail_in = inputLength; if (input.HasArray) { this.z.next_in = input.Array; this.z.next_in_index = input.ArrayOffset + input.ReaderIndex; } else { var array = new byte[inputLength]; input.GetBytes(input.ReaderIndex, array); this.z.next_in = array; this.z.next_in_index = 0; } int oldNextInIndex = this.z.next_in_index; // Configure output. int maxOutputLength = inputLength << 1; IByteBuffer decompressed = context.Allocator.Buffer(maxOutputLength); try { while (true) { this.z.avail_out = maxOutputLength; decompressed.EnsureWritable(maxOutputLength); this.z.next_out = decompressed.Array; this.z.next_out_index = decompressed.ArrayOffset + decompressed.WriterIndex; int oldNextOutIndex = this.z.next_out_index; // Decompress 'in' into 'out' int resultCode = this.z.Inflate(JZlib.Z_SYNC_FLUSH); int outputLength = this.z.next_out_index - oldNextOutIndex; if (outputLength > 0) { decompressed.SetWriterIndex(decompressed.WriterIndex + outputLength); } if (resultCode == JZlib.Z_NEED_DICT) { if (this.dictionary == null) { ZlibUtil.Fail(this.z, "decompression failure", resultCode); } else { resultCode = this.z.InflateSetDictionary(this.dictionary, this.dictionary.Length); if (resultCode != JZlib.Z_OK) { ZlibUtil.Fail(this.z, "failed to set the dictionary", resultCode); } } continue; } if (resultCode == JZlib.Z_STREAM_END) { this.finished = true; // Do not decode anymore. this.z.InflateEnd(); break; } if (resultCode == JZlib.Z_OK) { continue; } if (resultCode == JZlib.Z_BUF_ERROR) { if (this.z.avail_in <= 0) { break; } continue; } //default ZlibUtil.Fail(this.z, "decompression failure", resultCode); } } finally { input.SkipBytes(this.z.next_in_index - oldNextInIndex); if (decompressed.IsReadable()) { output.Add(decompressed); } else { decompressed.Release(); } } } finally { this.z.next_in = null; this.z.next_out = null; } }