Class LengthFieldPrepender
- java.lang.Object
-
- org.jboss.netty.handler.codec.oneone.OneToOneEncoder
-
- org.jboss.netty.handler.codec.frame.LengthFieldPrepender
-
- All Implemented Interfaces:
ChannelDownstreamHandler,ChannelHandler
@Sharable public class LengthFieldPrepender extends OneToOneEncoder
An encoder that prepends the length of the message. The length value is prepended as a binary form. It is encoded in either big endian or little endian depending on the defaultByteOrderof the currentChannelBufferFactory.For example,
LengthFieldPrepender(2) will encode the following 12-bytes string:+----------------+ | "HELLO, WORLD" | +----------------+
into the following:+--------+----------------+ + 0x000C | "HELLO, WORLD" | +--------+----------------+
If you turned on thelengthIncludesLengthFieldLengthflag 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 org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private intlengthFieldLengthprivate booleanlengthIncludesLengthFieldLength
-
Constructor Summary
Constructors Constructor Description LengthFieldPrepender(int lengthFieldLength)Creates a new instance.LengthFieldPrepender(int lengthFieldLength, boolean lengthIncludesLengthFieldLength)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Objectencode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg)Transforms the specified message into another message and return the transformed message.-
Methods inherited from class org.jboss.netty.handler.codec.oneone.OneToOneEncoder
doEncode, handleDownstream
-
-
-
-
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- iflengthFieldLengthis 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- iflengthFieldLengthis not 1, 2, 3, 4, or 8
-
-
Method Detail
-
encode
protected java.lang.Object encode(ChannelHandlerContext ctx, Channel channel, java.lang.Object msg) throws java.lang.Exception
Description copied from class:OneToOneEncoderTransforms the specified message into another message and return the transformed message. Note that you can not returnnull, unlike you can inOneToOneDecoder.decode(ChannelHandlerContext, Channel, Object); you must return something, at leastChannelBuffers.EMPTY_BUFFER.- Specified by:
encodein classOneToOneEncoder- Throws:
java.lang.Exception
-
-