Class 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 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" |
     +--------+----------------+
     
    • 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 - if true, 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 field
        lengthIncludesLengthFieldLength - if true, 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 - the ByteOrder of the length field
        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
        lengthIncludesLengthFieldLength - if true, 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
        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.
      • 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 - the ChannelHandlerContext which this LengthFieldPrepender belongs to
        length - the length which should be encoded in the length field
        lengthFieldLength - the length of the prepended length field
        byteOrder - the ByteOrder of the length field
        Returns:
        A buffer containing the encoded length
        Throws:
        EncoderException - if failed to encode the length