Top Description Fields Constructors Methods
io.netty.handler.codec.compression

public Class Lz4FrameEncoder

extends MessageToByteEncoder<ByteBuf>
Class Inheritance
Imports
io.netty.buffer.ByteBuf, .Unpooled, io.netty.channel.ChannelFuture, .ChannelHandlerContext, .ChannelPipeline, .ChannelPromise, io.netty.handler.codec.EncoderException, .MessageToByteEncoder, io.netty.util.concurrent.EventExecutor, .PromiseNotifier, io.netty.util.internal.ObjectUtil, net.jpountz.lz4.LZ4Compressor, .LZ4Exception, .LZ4Factory, java.nio.ByteBuffer, java.util.zip.Checksum

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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Field Summary

Modifier and TypeField and Description
private final int
private ByteBuf
buffer

Inner byte buffer for outgoing data.

private final ByteBufChecksum
checksum

Underlying checksum calculator in use.

private final int
compressionLevel

Compression level of current LZ4 encoder (depends on blockSize).

private final LZ4Compressor
compressor

Underlying compressor in use.

private volatile ChannelHandlerContext
ctx

Used to interact with its ChannelPipeline and other handlers.

pack-priv static final int
private volatile boolean
finished

Indicates if the compressed stream has been finished.

private final int
maxEncodeSize

Maximum size for any buffer to write encoded (compressed) data into.

Constructor Summary

AccessConstructor and Description
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.

public
Lz4FrameEncoder(boolean
if true codec will use compressor which requires more memory and is slower but compresses more efficiently
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.

public
Lz4FrameEncoder(LZ4Factory
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 sun.misc.Unsafe
factory
,
boolean
if true codec will use compressor which requires more memory and is slower but compresses more efficiently
highCompressor
,
int
the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M
blockSize
,
Checksum
the Checksum instance to use to check data for integrity
checksum
)

Creates a new customizable LZ4 encoder.

public
Lz4FrameEncoder(LZ4Factory
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 sun.misc.Unsafe
factory
,
boolean
if true codec will use compressor which requires more memory and is slower but compresses more efficiently
highCompressor
,
int
the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M
blockSize
,
Checksum
the Checksum instance to use to check data for integrity
checksum
,
int
the maximum size for an encode (compressed) buffer
maxEncodeSize
)

Creates a new customizable LZ4 encoder.

Method Summary

Modifier and TypeMethod and Description
protected ByteBuf
private ByteBuf
allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect, boolean allowEmptyReturn)

public ChannelFuture
close()

Close this Lz4FrameEncoder and so finish the encoding.

public ChannelFuture
close(final ChannelPromise promise)

Close this Lz4FrameEncoder and so finish the encoding.

public void
close(final ChannelHandlerContext
the ChannelHandlerContext for which the close operation is made
ctx
,
final ChannelPromise
the ChannelPromise to notify once the operation completes
promise
)

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline.
private static int
compressionLevel(int blockSize)

Calculates compression level on the basis of block size.

private ChannelHandlerContext
ctx()

protected void
encode(ChannelHandlerContext ctx, ByteBuf
the message to encode
in
,
ByteBuf
the ByteBuf into which the encoded message will be written
out
)

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a ByteBuf.
private ChannelFuture
public void
private void
pack-priv final ByteBuf
public void
handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.
public void
handlerRemoved(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.
public boolean
isClosed()

Returns true if and only if the compressed stream has been finished.

Inherited from io.netty.handler.codec.MessageToByteEncoder:
acceptOutboundMessageisPreferDirectwrite

Field Detail

blockSizeback to summary
private final int blockSize
bufferback to summary
private ByteBuf buffer

Inner byte buffer for outgoing data. It's capacity will be blockSize.

checksumback to summary
private final ByteBufChecksum checksum

Underlying checksum calculator in use.

compressionLevelback to summary
private final int compressionLevel

Compression level of current LZ4 encoder (depends on blockSize).

compressorback to summary
private final LZ4Compressor compressor

Underlying compressor in use.

ctxback to summary
private volatile ChannelHandlerContext ctx

Used to interact with its ChannelPipeline and other handlers.

DEFAULT_MAX_ENCODE_SIZEback to summary
pack-priv static final int DEFAULT_MAX_ENCODE_SIZE
finishedback to summary
private volatile boolean finished

Indicates if the compressed stream has been finished.

maxEncodeSizeback to summary
private final int maxEncodeSize

Maximum size for any buffer to write encoded (compressed) data into.

Constructor Detail

Lz4FrameEncoderback to summary
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.

Lz4FrameEncoderback to summary
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:boolean

if true codec will use compressor which requires more memory and is slower but compresses more efficiently

Lz4FrameEncoderback to summary
public Lz4FrameEncoder(LZ4Factory factory, boolean highCompressor, int blockSize, Checksum checksum)

Creates a new customizable LZ4 encoder.

Parameters
factory:LZ4Factory

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 sun.misc.Unsafe

highCompressor:boolean

if true codec will use compressor which requires more memory and is slower but compresses more efficiently

blockSize:int

the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M

checksum:Checksum

the Checksum instance to use to check data for integrity

Lz4FrameEncoderback to summary
public Lz4FrameEncoder(LZ4Factory factory, boolean highCompressor, int blockSize, Checksum checksum, int maxEncodeSize)

Creates a new customizable LZ4 encoder.

Parameters
factory:LZ4Factory

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 sun.misc.Unsafe

highCompressor:boolean

if true codec will use compressor which requires more memory and is slower but compresses more efficiently

blockSize:int

the maximum number of bytes to try to compress at once, must be >= 64 and <= 32 M

checksum:Checksum

the Checksum instance to use to check data for integrity

maxEncodeSize:int

the maximum size for an encode (compressed) buffer

Method Detail

allocateBufferback to summary
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect)

Overrides io.netty.handler.codec.MessageToByteEncoder.allocateBuffer.

Doc from io.netty.handler.codec.MessageToByteEncoder.allocateBuffer.

Allocate a ByteBuf 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.

Annotations
@Override
allocateBufferback to summary
private ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect, boolean allowEmptyReturn)
closeback to summary
public ChannelFuture close()

Close this Lz4FrameEncoder and so finish the encoding. The returned ChannelFuture will be notified once the operation completes.

closeback to summary
public ChannelFuture close(final ChannelPromise promise)

Close this Lz4FrameEncoder and so finish the encoding. The given ChannelFuture will be notified once the operation completes and will also be returned.

closeback to summary
public void close(final ChannelHandlerContext ctx, final ChannelPromise promise) throws Exception

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.close.

Implements io.netty.channel.ChannelOutboundHandler.close.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.close.

Calls ChannelHandlerContext#close(ChannelPromise) to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelHandlerContext for which the close operation is made

promise:ChannelPromise

Doc from io.netty.channel.ChannelOutboundHandler.close.

the ChannelPromise to notify once the operation completes

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.close.

thrown if an error occurs

compressionLevelback to summary
private static int compressionLevel(int blockSize)

Calculates compression level on the basis of block size.

ctxback to summary
private ChannelHandlerContext ctx()
encodeback to summary
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception

Implements abstract io.netty.handler.codec.MessageToByteEncoder.encode.

Doc from io.netty.handler.codec.MessageToByteEncoder.encode.

Encode a message into a 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.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToByteEncoder belongs to

in:ByteBuf

the message to encode

out:ByteBuf

the ByteBuf into which the encoded message will be written

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs
finishEncodeback to summary
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise)
flushback to summary
public void flush(final ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelOutboundHandlerAdapter.flush.

Implements io.netty.channel.ChannelOutboundHandler.flush.

Doc from io.netty.channel.ChannelOutboundHandlerAdapter.flush.

Calls ChannelHandlerContext#flush() to forward to the next ChannelOutboundHandler in the ChannelPipeline. Sub-classes may override this method to change behavior.

Parameters
ctx:ChannelHandlerContext

Doc from io.netty.channel.ChannelOutboundHandler.flush.

the ChannelHandlerContext for which the flush operation is made

Annotations
@Override
Exceptions
Exception:

Doc from io.netty.channel.ChannelOutboundHandler.flush.

thrown if an error occurs

flushBufferedDataback to summary
private void flushBufferedData(ByteBuf out)
getBackingBufferback to summary
pack-priv final ByteBuf getBackingBuffer()
handlerAddedback to summary
public void handlerAdded(ChannelHandlerContext ctx)

Overrides io.netty.channel.ChannelHandlerAdapter.handlerAdded.

Implements io.netty.channel.ChannelHandler.handlerAdded.

Doc from io.netty.channel.ChannelHandler.handlerAdded.

Gets called after the ChannelHandler was added to the actual context and it's ready to handle events.

Annotations
@Override
handlerRemovedback to summary
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception

Overrides io.netty.channel.ChannelHandlerAdapter.handlerRemoved.

Implements io.netty.channel.ChannelHandler.handlerRemoved.

Doc from io.netty.channel.ChannelHandler.handlerRemoved.

Gets called after the ChannelHandler was removed from the actual context and it doesn't handle events anymore.

Annotations
@Override
isClosedback to summary
public boolean isClosed()

Returns true if and only if the compressed stream has been finished.