- java.lang.Object
-
- io.netty5.channel.ChannelHandlerAdapter
-
- io.netty5.handler.codec.MessageToMessageEncoder<Buffer>
-
- io.netty5.handler.codec.LengthFieldPrepender
-
- All Implemented Interfaces:
ChannelHandler
public class LengthFieldPrepender extends MessageToMessageEncoder<Buffer>
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 thelengthIncludesLengthFieldLength
flag in the constructor, the encoded data would look like the following (12 (original data) + 2 (prepended data) = 14 (0xE)):+--------+----------------+ + 0x000E | "HELLO, WORLD" | +--------+----------------+
-
-
Constructor Summary
Constructors Constructor Description LengthFieldPrepender(int lengthFieldLength)
Creates a new instance.LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength)
Creates a new instance.LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment)
Creates a new instance.LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
Creates a new instance.LengthFieldPrepender(ByteOrder byteOrder, int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
encode(ChannelHandlerContext ctx, Buffer buffer, List<Object> out)
Encode from one message to another.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.boolean
isSharable()
Returnstrue
if this handler is sharable and thus can be added to more than oneChannelPipeline
.-
Methods inherited from class io.netty5.handler.codec.MessageToMessageEncoder
acceptOutboundMessage, encodeAndClose, write
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.netty5.channel.ChannelHandler
bind, channelActive, channelExceptionCaught, channelInactive, channelInboundEvent, channelRead, channelReadComplete, channelRegistered, channelShutdown, channelUnregistered, channelWritabilityChanged, close, connect, deregister, disconnect, flush, handlerAdded, handlerRemoved, pendingOutboundBytes, read, register, sendOutboundEvent, shutdown
-
-
-
-
Constructor Detail
-
LengthFieldPrepender
public LengthFieldPrepender(int lengthFieldLength)
Creates a new instance.- Parameters:
lengthFieldLength
- the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.
-
LengthFieldPrepender
public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength)
Creates a new instance.- Parameters:
lengthFieldLength
- the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.lengthIncludesLengthFieldLength
- iftrue
, the length of the prepended length field is added to the value of the prepended length field.
-
LengthFieldPrepender
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment)
Creates a new instance.- Parameters:
lengthFieldLength
- the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.lengthAdjustment
- the compensation value to add to the value of the length field
-
LengthFieldPrepender
public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
Creates a new instance.- Parameters:
lengthFieldLength
- the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.lengthAdjustment
- the compensation value to add to the value of the length fieldlengthIncludesLengthFieldLength
- iftrue
, the length of the prepended length field is added to the value of the prepended length field.
-
LengthFieldPrepender
public LengthFieldPrepender(ByteOrder byteOrder, int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
Creates a new instance.- Parameters:
byteOrder
- theByteOrder
of the length fieldlengthFieldLength
- the length of the prepended length field. Only 1, 2, 3, 4, and 8 are supported by default.lengthAdjustment
- the compensation value to add to the value of the length fieldlengthIncludesLengthFieldLength
- iftrue
, the length of the prepended length field is added to the value of the prepended length field.
-
-
Method Detail
-
isSharable
public boolean isSharable()
Description copied from interface:ChannelHandler
Returnstrue
if this handler is sharable and thus can be added to more than oneChannelPipeline
. By default, this method returnsfalse
. If this method returnsfalse
, 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.
-
encode
protected void encode(ChannelHandlerContext ctx, Buffer buffer, List<Object> out) throws Exception
Description copied from class:MessageToMessageEncoder
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
MessageToMessageEncoder.encodeAndClose(ChannelHandlerContext, Object, List)
method.- Overrides:
encode
in classMessageToMessageEncoder<Buffer>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs to.buffer
- the message to encode to another one.out
- theList
into which produced output messages should be added.- Throws:
Exception
- is thrown if an error occurs.
-
getLengthFieldBuffer
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
- theChannelHandlerContext
which thisLengthFieldPrepender
belongs tolength
- the length which should be encoded in the length fieldlengthFieldLength
- the length of the prepended length fieldbyteOrder
- theByteOrder
of the length field- Returns:
- A buffer containing the encoded length
- Throws:
EncoderException
- if failed to encode the length
-
-