public static int WrapperOverhead(ZlibWrapper wrapper) { int overhead; switch (wrapper) { case ZlibWrapper.None: overhead = 0; break; case ZlibWrapper.Zlib: case ZlibWrapper.ZlibOrNone: overhead = 2; break; case ZlibWrapper.Gzip: overhead = 10; break; default: throw new NotSupportedException($"Unknown value {wrapper}"); } return(overhead); }
/** * 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) { if (compressionLevel < 0 || compressionLevel > 9) { CThrowHelper.ThrowArgumentException_CompressionLevel(compressionLevel); } if (windowBits < 9 || windowBits > 15) { CThrowHelper.ThrowArgumentException_WindowBits(windowBits); } if (memLevel < 1 || memLevel > 9) { CThrowHelper.ThrowArgumentException_MemLevel(memLevel); } 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); }
public static JZlib.WrapperType ConvertWrapperType(ZlibWrapper wrapper) { JZlib.WrapperType convertedWrapperType; switch (wrapper) { case ZlibWrapper.None: convertedWrapperType = JZlib.W_NONE; break; case ZlibWrapper.Zlib: convertedWrapperType = JZlib.W_ZLIB; break; case ZlibWrapper.Gzip: convertedWrapperType = JZlib.W_GZIP; break; case ZlibWrapper.ZlibOrNone: convertedWrapperType = JZlib.W_ANY; break; default: throw new ArgumentException($"Unknown type {wrapper}"); } return(convertedWrapperType); }
protected override EmbeddedChannel NewContentDecoder(ICharSequence contentEncoding) { if (HttpHeaderValues.Gzip.ContentEqualsIgnoreCase(contentEncoding) || HttpHeaderValues.XGzip.ContentEqualsIgnoreCase(contentEncoding)) { return(new EmbeddedChannel( this.HandlerContext.Channel.Id, this.HandlerContext.Channel.Metadata.HasDisconnect, this.HandlerContext.Channel.Configuration, ZlibCodecFactory.NewZlibDecoder(ZlibWrapper.Gzip))); } if (HttpHeaderValues.Deflate.ContentEqualsIgnoreCase(contentEncoding) || HttpHeaderValues.XDeflate.ContentEqualsIgnoreCase(contentEncoding)) { ZlibWrapper wrapper = this.strict ? ZlibWrapper.Zlib : ZlibWrapper.ZlibOrNone; return(new EmbeddedChannel( this.HandlerContext.Channel.Id, this.HandlerContext.Channel.Metadata.HasDisconnect, this.HandlerContext.Channel.Configuration, ZlibCodecFactory.NewZlibDecoder(wrapper))); } // 'identity' or unsupported return(null); }
/// <summary> /// Generate a new instance of an <see cref="EmbeddedChannel"/> capable of compressing data /// </summary> /// <param name="ctx">the context.</param> /// <param name="wrapper">Defines what type of encoder should be used</param> /// <returns></returns> private EmbeddedChannel NewCompressionChannel(IChannelHandlerContext ctx, ZlibWrapper wrapper) { var channel = ctx.Channel; return(new EmbeddedChannel(channel.Id, channel.Metadata.HasDisconnect, channel.Configuration, ZlibCodecFactory.NewZlibEncoder(wrapper, _compressionLevel, _windowBits, _memLevel))); }
public JZlibDecoder(ZlibWrapper wrapper) { int resultCode = this.z.Init(ZlibUtil.ConvertWrapperType(wrapper)); 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); }
/// <summary> /// Returns a new <see cref="EmbeddedChannel"/> that decodes the HTTP2 message content encoded in the specified /// <paramref name="contentEncoding"/>. /// </summary> /// <param name="ctx">The context</param> /// <param name="contentEncoding">the value of the <c>content-encoding</c> header</param> /// <returns>a new <see cref="ByteToMessageDecoder"/> if the specified encoding is supported. <c>null</c> otherwise /// (alternatively, you can throw a <see cref="Http2Exception"/> to block unknown encoding).</returns> /// <exception cref="Http2Exception">If the specified encoding is not not supported and warrants an exception.</exception> protected EmbeddedChannel NewContentDecompressor(IChannelHandlerContext ctx, ICharSequence contentEncoding) { var channel = ctx.Channel; if (HttpHeaderValues.Gzip.ContentEqualsIgnoreCase(contentEncoding) || HttpHeaderValues.XGzip.ContentEqualsIgnoreCase(contentEncoding)) { return(new EmbeddedChannel(channel.Id, channel.Metadata.HasDisconnect, channel.Configuration, ZlibCodecFactory.NewZlibDecoder(ZlibWrapper.Gzip))); } if (HttpHeaderValues.Deflate.ContentEqualsIgnoreCase(contentEncoding) || HttpHeaderValues.XDeflate.ContentEqualsIgnoreCase(contentEncoding)) { ZlibWrapper wrapper = _strict ? ZlibWrapper.Zlib : ZlibWrapper.ZlibOrNone; // To be strict, 'deflate' means ZLIB, but some servers were not implemented correctly. return(new EmbeddedChannel(channel.Id, channel.Metadata.HasDisconnect, channel.Configuration, ZlibCodecFactory.NewZlibDecoder(wrapper))); } // 'identity' or unsupported return(null); }
public static ZlibDecoder NewZlibDecoder(ZlibWrapper wrapper) => new JZlibDecoder(wrapper);
public static ZlibEncoder NewZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) => new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
public static ZlibEncoder NewZlibEncoder(ZlibWrapper wrapper, int compressionLevel) => new JZlibEncoder(wrapper, compressionLevel);
/// <summary> /// Creates a new instance with the specified wrapper. /// </summary> /// <param name="wrapper"></param> public JZlibDecoder(ZlibWrapper wrapper) : this(wrapper, 0) { }
public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel) : this(wrapper, compressionLevel, 15, 8) { }
public JZlibEncoder(ZlibWrapper wrapper) : this(wrapper, 6) { }