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
Compresses a
ByteBuf using 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 -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final intprivate ByteBufInner byte buffer for outgoing data.private final ByteBufChecksumUnderlying checksum calculator in use.private final intCompression level of current LZ4 encoder (depends onblockSize).private final net.jpountz.lz4.LZ4CompressorUnderlying compressor in use.private ChannelHandlerContextUsed to interact with itsChannelPipelineand other handlers.(package private) static final intprivate booleanIndicates if the compressed stream has been finished.private final intMaximum size for any buffer to write encoded (compressed) data into. -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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, Checksum checksum) Creates a new customizable LZ4 encoder.Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, Checksum checksum, int maxEncodeSize) Creates a new customizable LZ4 encoder. -
Method Summary
Modifier and TypeMethodDescriptionprotected ByteBufallocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) Allocate aByteBufwhich will be used as argument ofMessageToByteEncoder.encode(ChannelHandlerContext, I, ByteBuf).private ByteBufallocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect, boolean allowEmptyReturn) close()Close thisLz4FrameEncoderand so finish the encoding.voidclose(ChannelHandlerContext ctx, ChannelPromise promise) CallsChannelOutboundInvoker.close(ChannelPromise)to forward to the nextChannelOutboundHandlerin theChannelPipeline.close(ChannelPromise promise) Close thisLz4FrameEncoderand so finish the encoding.private static intcompressionLevel(int blockSize) Calculates compression level on the basis of block size.private ChannelHandlerContextctx()protected voidencode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) Encode a message into aByteBuf.private ChannelFuturefinishEncode(ChannelHandlerContext ctx, ChannelPromise promise) voidCallsChannelHandlerContext.flush()to forward to the nextChannelOutboundHandlerin theChannelPipeline.private voidflushBufferedData(ByteBuf out) (package private) final ByteBufvoidDo nothing by default, sub-classes may override this method.voidDo 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, writeMethods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, connect, deregister, disconnect, readMethods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, isSharableMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.netty.channel.ChannelHandler
exceptionCaught
-
Field Details
-
DEFAULT_MAX_ENCODE_SIZE
static final int DEFAULT_MAX_ENCODE_SIZE- See Also:
-
blockSize
private final int blockSize -
compressor
private final net.jpountz.lz4.LZ4Compressor compressorUnderlying compressor in use. -
checksum
Underlying checksum calculator in use. -
compressionLevel
private final int compressionLevelCompression level of current LZ4 encoder (depends onblockSize). -
buffer
Inner byte buffer for outgoing data. It's capacity will beblockSize. -
maxEncodeSize
private final int maxEncodeSizeMaximum size for any buffer to write encoded (compressed) data into. -
finished
private volatile boolean finishedIndicates if the compressed stream has been finished. -
ctx
Used to interact with itsChannelPipelineand other handlers.
-
-
Constructor Details
-
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, 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 invalid input: '<'= 32 Mchecksum- theChecksuminstance to use to check data for integrity
-
Lz4FrameEncoder
public Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory, boolean highCompressor, int blockSize, 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 invalid input: '<'= 32 Mchecksum- theChecksuminstance to use to check data for integritymaxEncodeSize- the maximum size for an encode (compressed) buffer
-
-
Method Details
-
compressionLevel
private static int compressionLevel(int blockSize) Calculates compression level on the basis of block size. -
allocateBuffer
Description copied from class:MessageToByteEncoderAllocate aByteBufwhich will be used as argument ofMessageToByteEncoder.encode(ChannelHandlerContext, I, ByteBuf). Sub-classes may override this method to returnByteBufwith a perfect matchinginitialCapacity.- Overrides:
allocateBufferin classMessageToByteEncoder<ByteBuf>
-
allocateBuffer
private ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect, boolean allowEmptyReturn) -
encode
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:
Exception- is thrown if an error occurs
-
flushBufferedData
-
flush
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:
Exception- thrown if an error occurs
-
finishEncode
-
isClosed
public boolean isClosed()Returnstrueif and only if the compressed stream has been finished. -
close
Close thisLz4FrameEncoderand so finish the encoding. The returnedChannelFuturewill be notified once the operation completes. -
close
Close thisLz4FrameEncoderand so finish the encoding. The givenChannelFuturewill be notified once the operation completes and will also be returned. -
close
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:
Exception- thrown if an error occurs
-
ctx
-
handlerAdded
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerAddedin interfaceChannelHandler- Overrides:
handlerAddedin classChannelHandlerAdapter
-
handlerRemoved
Description copied from class:ChannelHandlerAdapterDo nothing by default, sub-classes may override this method.- Specified by:
handlerRemovedin interfaceChannelHandler- Overrides:
handlerRemovedin classChannelHandlerAdapter- Throws:
Exception
-
getBackingBuffer
-