@ChannelHandler.Sharable public class LengthFieldPrepender extends MessageToByteEncoder<ByteBuf>
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" | +--------+----------------+
ChannelHandler.Sharable
Constructor and Description |
---|
LengthFieldPrepender(ByteOrder byteOrder,
int lengthFieldLength,
int lengthAdjustment,
boolean lengthIncludesLengthFieldLength)
Creates a new instance.
|
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.
|
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) . |
protected void |
encode(ChannelHandlerContext ctx,
ByteBuf msg,
ByteBuf out)
Encode a message into a
ByteBuf . |
acceptOutboundMessage, write
bind, close, connect, deregister, disconnect, flush, read
exceptionCaught, handlerAdded, handlerRemoved, isSharable
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
exceptionCaught, handlerAdded, handlerRemoved
public LengthFieldPrepender(int lengthFieldLength)
lengthFieldLength
- the length of the prepended length field.
Only 1, 2, 3, 4, and 8 are allowed.IllegalArgumentException
- if lengthFieldLength
is not 1, 2, 3, 4, or 8public LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength)
lengthFieldLength
- the length of the prepended length field.
Only 1, 2, 3, 4, and 8 are allowed.lengthIncludesLengthFieldLength
- if true
, the length of the prepended
length field is added to the value of the
prepended length field.IllegalArgumentException
- if lengthFieldLength
is not 1, 2, 3, 4, or 8public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment)
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 fieldIllegalArgumentException
- if lengthFieldLength
is not 1, 2, 3, 4, or 8public LengthFieldPrepender(int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
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
- if true
, the length of the prepended
length field is added to the value of the
prepended length field.IllegalArgumentException
- if lengthFieldLength
is not 1, 2, 3, 4, or 8public LengthFieldPrepender(ByteOrder byteOrder, int lengthFieldLength, int lengthAdjustment, boolean lengthIncludesLengthFieldLength)
byteOrder
- the ByteOrder
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
- if true
, the length of the prepended
length field is added to the value of the
prepended length field.IllegalArgumentException
- if lengthFieldLength
is not 1, 2, 3, 4, or 8protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception
MessageToByteEncoder
ByteBuf
. This method will be called for each written message that can be handled
by this encoder.encode
in class MessageToByteEncoder<ByteBuf>
ctx
- the ChannelHandlerContext
which this MessageToByteEncoder
belongs tomsg
- the message to encodeout
- the ByteBuf
into which the encoded message will be writtenException
- is thrown if an error accourprotected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception
MessageToByteEncoder
ByteBuf
which will be used as argument of #encode(ChannelHandlerContext, I, ByteBuf)
.
Sub-classes may override this method to returna ByteBuf
with a perfect matching initialCapacity
.allocateBuffer
in class MessageToByteEncoder<ByteBuf>
Exception
Copyright © 2008–2016 The Netty Project. All rights reserved.