Class JdkZlibEncoder
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToByteEncoder<ByteBuf>
-
- io.netty.handler.codec.compression.ZlibEncoder
-
- io.netty.handler.codec.compression.JdkZlibEncoder
-
- All Implemented Interfaces:
ChannelHandler,ChannelOutboundHandler
public class JdkZlibEncoder extends ZlibEncoder
Compresses aByteBufusing the deflate algorithm.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.zip.CRC32crcprivate ChannelHandlerContextctxprivate java.util.zip.Deflaterdeflaterprivate booleanfinishedprivate static byte[]gzipHeaderprivate ZlibWrapperwrapperprivate booleanwriteHeader
-
Constructor Summary
Constructors Constructor Description JdkZlibEncoder()Creates a new zlib encoder with the default compression level (6) and the default wrapper (ZlibWrapper.ZLIB).JdkZlibEncoder(byte[] dictionary)Creates a new zlib encoder with the default compression level (6) and the specified preset dictionary.JdkZlibEncoder(int compressionLevel)Creates a new zlib encoder with the specifiedcompressionLeveland the default wrapper (ZlibWrapper.ZLIB).JdkZlibEncoder(int compressionLevel, byte[] dictionary)Creates a new zlib encoder with the specifiedcompressionLeveland the specified preset dictionary.JdkZlibEncoder(ZlibWrapper wrapper)Creates a new zlib encoder with the default compression level (6) and the specified wrapper.JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel)Creates a new zlib encoder with the specifiedcompressionLeveland the specified wrapper.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected ByteBufallocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)Allocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf).ChannelFutureclose()Close thisZlibEncoderand so finish the encoding.voidclose(ChannelHandlerContext ctx, ChannelPromise promise)CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.ChannelFutureclose(ChannelPromise promise)Close thisZlibEncoderand so finish the encoding.private ChannelHandlerContextctx()private voiddeflate(ByteBuf out)private voiddeflateJdk6(ByteBuf out)protected voidencode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out)Encode a message into aByteBuf.private ChannelFuturefinishEncode(ChannelHandlerContext ctx, ChannelPromise promise)voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.booleanisClosed()Returnstrueif and only if the end of the compressed stream has been reached.-
Methods inherited from class io.netty.handler.codec.MessageToByteEncoder
acceptOutboundMessage, isPreferDirect, write
-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerRemoved, isSharable
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught, handlerRemoved
-
-
-
-
Field Detail
-
wrapper
private final ZlibWrapper wrapper
-
deflater
private final java.util.zip.Deflater deflater
-
finished
private volatile boolean finished
-
ctx
private volatile ChannelHandlerContext ctx
-
crc
private final java.util.zip.CRC32 crc
-
gzipHeader
private static final byte[] gzipHeader
-
writeHeader
private boolean writeHeader
-
-
Constructor Detail
-
JdkZlibEncoder
public JdkZlibEncoder()
Creates a new zlib encoder with the default compression level (6) and the default wrapper (ZlibWrapper.ZLIB).- Throws:
CompressionException- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLeveland the default wrapper (ZlibWrapper.ZLIB).- Parameters:
compressionLevel-1yields the fastest compression and9yields the best compression.0means no compression. The default compression level is6.- Throws:
CompressionException- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(ZlibWrapper wrapper)
Creates a new zlib encoder with the default compression level (6) and the specified wrapper.- Throws:
CompressionException- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(ZlibWrapper wrapper, int compressionLevel)
Creates a new zlib encoder with the specifiedcompressionLeveland the specified wrapper.- Parameters:
compressionLevel-1yields the fastest compression and9yields the best compression.0means no compression. The default compression level is6.- Throws:
CompressionException- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(byte[] dictionary)
Creates a new zlib encoder with the default compression level (6) and the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIBbecause it is the only format that supports the preset dictionary.- Parameters:
dictionary- the preset dictionary- Throws:
CompressionException- if failed to initialize zlib
-
JdkZlibEncoder
public JdkZlibEncoder(int compressionLevel, byte[] dictionary)Creates a new zlib encoder with the specifiedcompressionLeveland the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIBbecause it is the only format that supports the preset dictionary.- Parameters:
compressionLevel-1yields the fastest compression and9yields the best compression.0means no compression. The default compression level is6.dictionary- the preset dictionary- Throws:
CompressionException- if failed to initialize zlib
-
-
Method Detail
-
close
public ChannelFuture close()
Description copied from class:ZlibEncoderClose thisZlibEncoderand so finish the encoding. The returnedChannelFuturewill be notified once the operation completes.- Specified by:
closein classZlibEncoder
-
close
public ChannelFuture close(ChannelPromise promise)
Description copied from class:ZlibEncoderClose thisZlibEncoderand so finish the encoding. The givenChannelFuturewill be notified once the operation completes and will also be returned.- Specified by:
closein classZlibEncoder
-
ctx
private ChannelHandlerContext ctx()
-
isClosed
public boolean isClosed()
Description copied from class:ZlibEncoderReturnstrueif and only if the end of the compressed stream has been reached.- Specified by:
isClosedin classZlibEncoder
-
encode
protected void encode(ChannelHandlerContext ctx, ByteBuf uncompressed, ByteBuf out) throws java.lang.Exception
Description copied from class:MessageToByteEncoderEncode a message into aByteBuf. This method will be called for each written message that can be handled by this encoder.- Specified by:
encodein classMessageToByteEncoder<ByteBuf>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToByteEncoderbelongs touncompressed- the message to encodeout- theByteBufinto which the encoded message will be written- Throws:
java.lang.Exception- is thrown if an error occurs
-
allocateBuffer
protected final ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws java.lang.Exception
Description copied from class:MessageToByteEncoderAllocate aByteBufwhich will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to returnByteBufwith a perfect matchinginitialCapacity.- Overrides:
allocateBufferin classMessageToByteEncoder<ByteBuf>- Throws:
java.lang.Exception
-
close
public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
closein interfaceChannelOutboundHandler- Overrides:
closein classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the close operation is madepromise- theChannelPromiseto notify once the operation completes- Throws:
java.lang.Exception- thrown if an error occurs
-
finishEncode
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise)
-
deflate
private void deflate(ByteBuf out)
-
deflateJdk6
private void deflateJdk6(ByteBuf out)
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
-