public class Lz4FrameEncoder extends MessageToByteEncoder<ByteBuf>
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 *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
private int |
blockSize |
private ByteBuf |
buffer
Inner byte buffer for outgoing data.
|
private ByteBufChecksum |
checksum
Underlying checksum calculator in use.
|
private int |
compressionLevel
Compression level of current LZ4 encoder (depends on
blockSize). |
private net.jpountz.lz4.LZ4Compressor |
compressor
Underlying compressor in use.
|
private ChannelHandlerContext |
ctx
Used to interact with its
ChannelPipeline and other handlers. |
(package private) static int |
DEFAULT_MAX_ENCODE_SIZE |
private static EncoderException |
ENCODE_FINSHED_EXCEPTION |
private boolean |
finished
Indicates if the compressed stream has been finished.
|
private int |
maxEncodeSize
Maximum size for any buffer to write encoded (compressed) data into.
|
| Constructor and 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.
|
| Modifier and Type | Method and Description |
|---|---|
protected ByteBuf |
allocateBuffer(ChannelHandlerContext ctx,
ByteBuf msg,
boolean preferDirect)
Allocate a
ByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf). |
private ByteBuf |
allocateBuffer(ChannelHandlerContext ctx,
ByteBuf msg,
boolean preferDirect,
boolean allowEmptyReturn) |
ChannelFuture |
close()
Close this
Lz4FrameEncoder and so finish the encoding. |
void |
close(ChannelHandlerContext ctx,
ChannelPromise promise)
Calls
ChannelOutboundInvoker.close(ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
ChannelFuture |
close(ChannelPromise promise)
Close this
Lz4FrameEncoder and so finish the encoding. |
private static int |
compressionLevel(int blockSize)
Calculates compression level on the basis of block size.
|
private ChannelHandlerContext |
ctx() |
protected void |
encode(ChannelHandlerContext ctx,
ByteBuf in,
ByteBuf out)
Encode a message into a
ByteBuf. |
private ChannelFuture |
finishEncode(ChannelHandlerContext ctx,
ChannelPromise promise) |
void |
flush(ChannelHandlerContext ctx)
Calls
ChannelHandlerContext.flush() to forward
to the next ChannelOutboundHandler in the ChannelPipeline. |
private void |
flushBufferedData(ByteBuf out) |
(package private) ByteBuf |
getBackingBuffer() |
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()
Returns
true if and only if the compressed stream has been finished. |
acceptOutboundMessage, isPreferDirect, writebind, connect, deregister, disconnect, readensureNotSharable, exceptionCaught, isSharableclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitexceptionCaughtprivate static final EncoderException ENCODE_FINSHED_EXCEPTION
static final int DEFAULT_MAX_ENCODE_SIZE
private final int blockSize
private final net.jpountz.lz4.LZ4Compressor compressor
private final ByteBufChecksum checksum
private final int compressionLevel
blockSize).private final int maxEncodeSize
private volatile boolean finished
private volatile ChannelHandlerContext ctx
ChannelPipeline and other handlers.public Lz4FrameEncoder()
public Lz4FrameEncoder(boolean highCompressor)
highCompressor - if true codec will use compressor which requires more memory
and is slower but compresses more efficientlypublic Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory,
boolean highCompressor,
int blockSize,
java.util.zip.Checksum checksum)
factory - user customizable LZ4Factory instance
which may be JNI bindings to the original C implementation, a pure Java implementation
or a Java implementation that uses the UnsafehighCompressor - if true 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 - the Checksum instance to use to check data for integritypublic Lz4FrameEncoder(net.jpountz.lz4.LZ4Factory factory,
boolean highCompressor,
int blockSize,
java.util.zip.Checksum checksum,
int maxEncodeSize)
factory - user customizable LZ4Factory instance
which may be JNI bindings to the original C implementation, a pure Java implementation
or a Java implementation that uses the UnsafehighCompressor - if true 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 - the Checksum instance to use to check data for integritymaxEncodeSize - the maximum size for an encode (compressed) bufferprivate static int compressionLevel(int blockSize)
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)
MessageToByteEncoderByteBuf which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf).
Sub-classes may override this method to return ByteBuf with a perfect matching initialCapacity.allocateBuffer in class MessageToByteEncoder<ByteBuf>private ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect, boolean allowEmptyReturn)
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws java.lang.Exception
ByteBuf. This method will be called for each written message that can be handled
by this encoder.
Encodes the input buffer into blockSize chunks in the output buffer. Data is only compressed and
written once we hit the blockSize; else, it is copied into the backing buffer to await
more data.encode in class MessageToByteEncoder<ByteBuf>ctx - the ChannelHandlerContext which this MessageToByteEncoder belongs toin - the message to encodeout - the ByteBuf into which the encoded message will be writtenjava.lang.Exception - is thrown if an error occursprivate void flushBufferedData(ByteBuf out)
public void flush(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelOutboundHandlerAdapterChannelHandlerContext.flush() to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.flush in interface ChannelOutboundHandlerflush in class ChannelOutboundHandlerAdapterctx - the ChannelHandlerContext for which the flush operation is madejava.lang.Exception - thrown if an error occursprivate ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise)
public boolean isClosed()
true if and only if the compressed stream has been finished.public ChannelFuture close()
Lz4FrameEncoder and so finish the encoding.
The returned ChannelFuture will be notified once the operation completes.public ChannelFuture close(ChannelPromise promise)
Lz4FrameEncoder and so finish the encoding.
The given ChannelFuture will be notified once the operation
completes and will also be returned.public void close(ChannelHandlerContext ctx, ChannelPromise promise) throws java.lang.Exception
ChannelOutboundHandlerAdapterChannelOutboundInvoker.close(ChannelPromise) to forward
to the next ChannelOutboundHandler in the ChannelPipeline.
Sub-classes may override this method to change behavior.close in interface ChannelOutboundHandlerclose in class ChannelOutboundHandlerAdapterctx - the ChannelHandlerContext for which the close operation is madepromise - the ChannelPromise to notify once the operation completesjava.lang.Exception - thrown if an error occursprivate ChannelHandlerContext ctx()
public void handlerAdded(ChannelHandlerContext ctx)
ChannelHandlerAdapterhandlerAdded in interface ChannelHandlerhandlerAdded in class ChannelHandlerAdapterpublic void handlerRemoved(ChannelHandlerContext ctx) throws java.lang.Exception
ChannelHandlerAdapterhandlerRemoved in interface ChannelHandlerhandlerRemoved in class ChannelHandlerAdapterjava.lang.Exceptionfinal ByteBuf getBackingBuffer()