Class Lz4FrameEncoder
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToByteEncoder<ByteBuf>
-
- io.netty.handler.codec.compression.Lz4FrameEncoder
-
- All Implemented Interfaces:
ChannelHandler,ChannelOutboundHandler
public class Lz4FrameEncoder extends MessageToByteEncoder<ByteBuf>
Compresses aByteBufusing the LZ4 format. See original LZ4 Github project and LZ4 block format for full description. Since the original LZ4 block format does not contains size of compressed block and size of original data this encoder uses format like LZ4 Java library written by Adrien Grand and approved by Yann Collet (author of original LZ4 library). * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Magic * Token * Compressed * Decompressed * Checksum * + * LZ4 compressed * * * * length * length * * * block * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Constructor Summary
Constructors Constructor Description Lz4FrameEncoder()Creates the fastest LZ4 encoder with default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.Lz4FrameEncoder(boolean highCompressor)Creates a new LZ4 encoder with hight or fast compression, default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, java.util.zip.Checksum checksum)Creates a new customizable LZ4 encoder.Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, java.util.zip.Checksum checksum, int maxEncodeSize)Creates a new customizable LZ4 encoder.
-
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 thisLz4FrameEncoderand so finish the encoding.voidclose(ChannelHandlerContext ctx, ChannelPromise promise)CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.ChannelFutureclose(ChannelPromise promise)Close thisLz4FrameEncoderand so finish the encoding.protected voidencode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out)Encode a message into aByteBuf.voidflush(ChannelHandlerContext ctx)CallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.voidhandlerAdded(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.voidhandlerRemoved(ChannelHandlerContext ctx)Do nothing by default, sub-classes may override this method.booleanisClosed()Returnstrueif and only if the compressed stream has been finished.-
Methods inherited from class io.netty.handler.codec.MessageToByteEncoder
acceptOutboundMessage, isPreferDirect, write
-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, connect, deregister, disconnect, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, 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
-
-
-
-
Constructor Detail
-
Lz4FrameEncoder
public Lz4FrameEncoder()
Creates the fastest LZ4 encoder with default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.
-
Lz4FrameEncoder
public Lz4FrameEncoder(boolean highCompressor)
Creates a new LZ4 encoder with hight or fast compression, default block size (64 KB) and xxhash hashing for Java, based on Yann Collet's work available at Github.- Parameters:
highCompressor- iftruecodec will use compressor which requires more memory and is slower but compresses more efficiently
-
Lz4FrameEncoder
public Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, java.util.zip.Checksum checksum)Creates a new customizable LZ4 encoder.- Parameters:
factory- user customizableLZ4Factoryinstance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses theUnsafehighCompressor- iftruecodec will use compressor which requires more memory and is slower but compresses more efficientlyblockSize- the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 Mchecksum- theChecksuminstance to use to check data for integrity
-
Lz4FrameEncoder
public Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, java.util.zip.Checksum checksum, int maxEncodeSize)Creates a new customizable LZ4 encoder.- Parameters:
factory- user customizableLZ4Factoryinstance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses theUnsafehighCompressor- iftruecodec will use compressor which requires more memory and is slower but compresses more efficientlyblockSize- the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 Mchecksum- theChecksuminstance to use to check data for integritymaxEncodeSize- the maximum size for an encode (compressed) buffer
-
-
Method Detail
-
allocateBuffer
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)
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>
-
encode
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws java.lang.Exception
Encode a message into aByteBuf. This method will be called for each written message that can be handled by this encoder. Encodes the input buffer intoblockSizechunks in the output buffer. Data is only compressed and written once we hit theblockSize; else, it is copied into the backingbufferto await more data.- Specified by:
encodein classMessageToByteEncoder<ByteBuf>- Parameters:
ctx- theChannelHandlerContextwhich thisMessageToByteEncoderbelongs toin- the message to encodeout- theByteBufinto which the encoded message will be written- Throws:
java.lang.Exception- is thrown if an error occurs
-
flush
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelOutboundHandlerAdapterCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline. Sub-classes may override this method to change behavior.- Specified by:
flushin interfaceChannelOutboundHandler- Overrides:
flushin classChannelOutboundHandlerAdapter- Parameters:
ctx- theChannelHandlerContextfor which the flush operation is made- Throws:
java.lang.Exception- thrown if an error occurs
-
isClosed
public boolean isClosed()
Returnstrueif and only if the compressed stream has been finished.
-
close
public ChannelFuture close()
Close thisLz4FrameEncoderand so finish the encoding. The returnedChannelFuturewill be notified once the operation completes.
-
close
public ChannelFuture close(ChannelPromise promise)
Close thisLz4FrameEncoderand so finish the encoding. The givenChannelFuturewill be notified once the operation completes and will also be returned.
-
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
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx)
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
java.lang.Exception
-
-