Package io.netty.handler.codec
Class LengthFieldPrepender
- java.lang.Object
-
- io.netty.channel.ChannelHandlerAdapter
-
- io.netty.channel.ChannelOutboundHandlerAdapter
-
- io.netty.handler.codec.MessageToMessageEncoder<ByteBuf>
-
- io.netty.handler.codec.LengthFieldPrepender
-
- All Implemented Interfaces:
ChannelHandler
,ChannelOutboundHandler
@Sharable public class LengthFieldPrepender extends MessageToMessageEncoder<ByteBuf>
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" | +--------+----------------+
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
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(java.nio.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, ByteBuf msg, java.util.List<java.lang.Object> out)
Encode from one message to an other.-
Methods inherited from class io.netty.handler.codec.MessageToMessageEncoder
acceptOutboundMessage, write
-
Methods inherited from class io.netty.channel.ChannelOutboundHandlerAdapter
bind, close, connect, deregister, disconnect, flush, read
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, exceptionCaught, handlerAdded, handlerRemoved, 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, handlerAdded, handlerRemoved
-
-
-
-
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 allowed.- Throws:
java.lang.IllegalArgumentException
- iflengthFieldLength
is not 1, 2, 3, 4, or 8
-
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 allowed.lengthIncludesLengthFieldLength
- iftrue
, the length of the prepended length field is added to the value of the prepended length field.- Throws:
java.lang.IllegalArgumentException
- iflengthFieldLength
is not 1, 2, 3, 4, or 8
-
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 allowed.lengthAdjustment
- the compensation value to add to the value of the length field- Throws:
java.lang.IllegalArgumentException
- iflengthFieldLength
is not 1, 2, 3, 4, or 8
-
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 allowed.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.- Throws:
java.lang.IllegalArgumentException
- iflengthFieldLength
is not 1, 2, 3, 4, or 8
-
LengthFieldPrepender
public LengthFieldPrepender(java.nio.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 allowed.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.- Throws:
java.lang.IllegalArgumentException
- iflengthFieldLength
is not 1, 2, 3, 4, or 8
-
-
Method Detail
-
encode
protected void encode(ChannelHandlerContext ctx, ByteBuf msg, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:MessageToMessageEncoder
Encode from one message to an other. This method will be called for each written message that can be handled by this encoder.- Specified by:
encode
in classMessageToMessageEncoder<ByteBuf>
- Parameters:
ctx
- theChannelHandlerContext
which thisMessageToMessageEncoder
belongs tomsg
- the message to encode to an other oneout
- theList
into which the encoded msg should be added needs to do some kind of aggregation- Throws:
java.lang.Exception
- is thrown if an error occurs
-
-