Top Description Fields Constructors Methods
io.netty5.handler.codec

public Class LengthFieldPrepender

extends MessageToMessageEncoder<Buffer>
Class Inheritance
Imports
io.netty5.buffer.BufferUtil, .Buffer, io.netty5.channel.ChannelHandlerContext, java.nio.ByteOrder, java.util.List

An encoder that prepends the length of the message. The length value is prepended as a binary form.

For example, LengthFieldPrepender(2) will encode the following 12-bytes string:

+----------------+
| "HELLO, WORLD" |
+----------------+
into the following:
+--------+----------------+
+ 0x000C | "HELLO, WORLD" |
+--------+----------------+
If you turned on the lengthIncludesLengthFieldLength flag in the constructor, the encoded data would look like the following (12 (original data) + 2 (prepended data) = 14 (0xE)):
+--------+----------------+
+ 0x000E | "HELLO, WORLD" |
+--------+----------------+

Field Summary

Modifier and TypeField and Description
private final ByteOrder
private final int
private final int
private final boolean

Constructor Summary

AccessConstructor and Description
public
LengthFieldPrepender(int
the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
lengthFieldLength
)

Creates a new instance.

public
LengthFieldPrepender(int
the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
lengthFieldLength
,
boolean
if true, the length of the prepended length field is added to the value of the prepended length field.
lengthIncludesLengthFieldLength
)

Creates a new instance.

public
LengthFieldPrepender(int
the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
lengthFieldLength
,
int
the compensation value to add to the value of the length field
lengthAdjustment
)

Creates a new instance.

public
LengthFieldPrepender(int
the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
lengthFieldLength
,
int
the compensation value to add to the value of the length field
lengthAdjustment
,
boolean
if true, the length of the prepended length field is added to the value of the prepended length field.
lengthIncludesLengthFieldLength
)

Creates a new instance.

public
LengthFieldPrepender(ByteOrder
the ByteOrder of the length field
byteOrder
,
int
the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
lengthFieldLength
,
int
the compensation value to add to the value of the length field
lengthAdjustment
,
boolean
if true, the length of the prepended length field is added to the value of the prepended length field.
lengthIncludesLengthFieldLength
)

Creates a new instance.

Method Summary

Modifier and TypeMethod and Description
protected void
encode(ChannelHandlerContext ctx, Buffer
the message to encode to another one.
buffer
,
List<Object>
the List into which produced output messages should be added.
out
)

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to another.
protected Buffer

Returns:

A buffer containing the encoded length
getLengthFieldBuffer
(ChannelHandlerContext ctx, int
the length which should be encoded in the length field
length
,
int
the length of the prepended length field
lengthFieldLength
,
ByteOrder
the ByteOrder of the length field
byteOrder
)

Encodes the length into a buffer which will be prepended as the length field.

public boolean
isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline.
Inherited from io.netty5.handler.codec.MessageToMessageEncoder:
acceptOutboundMessageencodeAndClosewrite

Field Detail

byteOrderback to summary
private final ByteOrder byteOrder
lengthAdjustmentback to summary
private final int lengthAdjustment
lengthFieldLengthback to summary
private final int lengthFieldLength
lengthIncludesLengthFieldLengthback to summary
private final boolean lengthIncludesLengthFieldLength

Constructor Detail

LengthFieldPrependerback to summary
public LengthFieldPrepender(int lengthFieldLength)

Creates a new instance.

Parameters
lengthFieldLength:int

the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.

LengthFieldPrependerback to summary
public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength)

Creates a new instance.

Parameters
lengthFieldLength:int

the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.

lengthIncludesLengthFieldLength:boolean

if true, the length of the prepended length field is added to the value of the prepended length field.

LengthFieldPrependerback to summary
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment)

Creates a new instance.

Parameters
lengthFieldLength:int

the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.

lengthAdjustment:int

the compensation value to add to the value of the length field

LengthFieldPrependerback to summary
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)

Creates a new instance.

Parameters
lengthFieldLength:int

the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.

lengthAdjustment:int

the compensation value to add to the value of the length field

lengthIncludesLengthFieldLength:boolean

if true, the length of the prepended length field is added to the value of the prepended length field.

LengthFieldPrependerback to summary
public LengthFieldPrepender(ByteOrder byteOrder, int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)

Creates a new instance.

Parameters
byteOrder:ByteOrder

the ByteOrder of the length field

lengthFieldLength:int

the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.

lengthAdjustment:int

the compensation value to add to the value of the length field

lengthIncludesLengthFieldLength:boolean

if true, the length of the prepended length field is added to the value of the prepended length field.

Method Detail

encodeback to summary
protected void encode(ChannelHandlerContext ctx, Buffer buffer, List<Object> out) throws Exception

Overrides io.netty5.handler.codec.MessageToMessageEncoder.encode.

Doc from io.netty5.handler.codec.MessageToMessageEncoder.encode.

Encode from one message to another. This method will be called for each written message that can be handled by this encoder.

The message will be disposed of after this call.

Subclasses that wish to sometimes pass messages through, should instead override the encodeAndClose(ChannelHandlerContext, Object, List) method.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this MessageToMessageEncoder belongs to.

buffer:Buffer

the message to encode to another one.

out:List<Object>

the List into which produced output messages should be added.

Annotations
@Override
Exceptions
Exception:
is thrown if an error occurs.
getLengthFieldBufferback to summary
protected Buffer getLengthFieldBuffer(ChannelHandlerContext ctx, int length, int lengthFieldLength, ByteOrder byteOrder)

Encodes the length into a buffer which will be prepended as the length field. The default implementation is capable of encoding the length into an 8/16/24/32/64 bit integer. Override this method to encode the length field differently.

Parameters
ctx:ChannelHandlerContext

the ChannelHandlerContext which this LengthFieldPrepender belongs to

length:int

the length which should be encoded in the length field

lengthFieldLength:int

the length of the prepended length field

byteOrder:ByteOrder

the ByteOrder of the length field

Returns:Buffer

A buffer containing the encoded length

Exceptions
EncoderException:
if failed to encode the length
isSharableback to summary
public boolean isSharable()

Overrides default io.netty5.channel.ChannelHandler.isSharable.

Doc from io.netty5.channel.ChannelHandler.isSharable.

Returns true if this handler is sharable and thus can be added to more than one ChannelPipeline. By default, this method returns false. If this method returns false, you have to create a new handler instance every time you add it to a pipeline because it has unshared state such as member variables.

Annotations
@Override