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 aByteBuf
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
-
-
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 ByteBuf
allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)
Allocate aByteBuf
which will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf)
.ChannelFuture
close()
Close thisLz4FrameEncoder
and so finish the encoding.void
close(ChannelHandlerContext ctx, ChannelPromise promise)
CallsChannelOutboundInvoker.close(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.ChannelFuture
close(ChannelPromise promise)
Close thisLz4FrameEncoder
and so finish the encoding.protected void
encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out)
Encode a message into aByteBuf
.void
flush(ChannelHandlerContext ctx)
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
.void
handlerAdded(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.void
handlerRemoved(ChannelHandlerContext ctx)
Do nothing by default, sub-classes may override this method.boolean
isClosed()
Returnstrue
if 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
- iftrue
codec 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 customizableLZ4Factory
instance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses theUnsafe
highCompressor
- iftrue
codec 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
- theChecksum
instance 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 customizableLZ4Factory
instance which may be JNI bindings to the original C implementation, a pure Java implementation or a Java implementation that uses theUnsafe
highCompressor
- iftrue
codec 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
- theChecksum
instance 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:MessageToByteEncoder
Allocate aByteBuf
which will be used as argument of#encode(ChannelHandlerContext, I, ByteBuf)
. Sub-classes may override this method to returnByteBuf
with a perfect matchinginitialCapacity
.- Overrides:
allocateBuffer
in 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 intoblockSize
chunks in the output buffer. Data is only compressed and written once we hit theblockSize
; else, it is copied into the backingbuffer
to await more data.- Specified by:
encode
in classMessageToByteEncoder<ByteBuf>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToByteEncoder
belongs toin
- the message to encodeout
- theByteBuf
into 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:ChannelOutboundHandlerAdapter
CallsChannelHandlerContext.flush()
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
flush
in interfaceChannelOutboundHandler
- Overrides:
flush
in classChannelOutboundHandlerAdapter
- Parameters:
ctx
- theChannelHandlerContext
for which the flush operation is made- Throws:
java.lang.Exception
- thrown if an error occurs
-
isClosed
public boolean isClosed()
Returnstrue
if and only if the compressed stream has been finished.
-
close
public ChannelFuture close()
Close thisLz4FrameEncoder
and so finish the encoding. The returnedChannelFuture
will be notified once the operation completes.
-
close
public ChannelFuture close(ChannelPromise promise)
Close thisLz4FrameEncoder
and so finish the encoding. The givenChannelFuture
will 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:ChannelOutboundHandlerAdapter
CallsChannelOutboundInvoker.close(ChannelPromise)
to forward to the nextChannelOutboundHandler
in theChannelPipeline
. Sub-classes may override this method to change behavior.- Specified by:
close
in interfaceChannelOutboundHandler
- Overrides:
close
in classChannelOutboundHandlerAdapter
- Parameters:
ctx
- theChannelHandlerContext
for which the close operation is madepromise
- theChannelPromise
to notify once the operation completes- Throws:
java.lang.Exception
- thrown if an error occurs
-
handlerAdded
public void handlerAdded(ChannelHandlerContext ctx)
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerAdded
in interfaceChannelHandler
- Overrides:
handlerAdded
in classChannelHandlerAdapter
-
handlerRemoved
public void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
Description copied from class:ChannelHandlerAdapter
Do nothing by default, sub-classes may override this method.- Specified by:
handlerRemoved
in interfaceChannelHandler
- Overrides:
handlerRemoved
in classChannelHandlerAdapter
- Throws:
java.lang.Exception
-
-