Class MqttDecoder

    • Field Detail

      • variableHeader

        private java.lang.Object variableHeader
      • bytesRemainingInVariablePart

        private int bytesRemainingInVariablePart
      • maxBytesInMessage

        private final int maxBytesInMessage
      • maxClientIdLength

        private final int maxClientIdLength
      • strictUtf8Validation

        private final boolean strictUtf8Validation
      • utf8Decoder

        private java.nio.charset.CharsetDecoder utf8Decoder
    • Constructor Detail

      • MqttDecoder

        public MqttDecoder()
      • MqttDecoder

        public MqttDecoder​(int maxBytesInMessage)
      • MqttDecoder

        public MqttDecoder​(int maxBytesInMessage,
                           int maxClientIdLength)
      • MqttDecoder

        public MqttDecoder​(int maxBytesInMessage,
                           int maxClientIdLength,
                           boolean strictUtf8Validation)
        Creates a new MqttDecoder.
        Parameters:
        maxBytesInMessage - the maximum number of bytes a decoded message may consume.
        maxClientIdLength - the maximum length of the Client Identifier (CONNECT payload).
        strictUtf8Validation - if true (default), every UTF-8 Encoded String is validated according to MQTT 3.1.1 and MQTT 5.0 malformed UTF-8 sequences (including surrogates and overlong forms) and an embedded U+0000 are rejected as a Malformed Packet. If false, the legacy behaviour is preserved, malformed bytes are silently replaced with U+FFFD and U+0000 is accepted.
    • Method Detail

      • invalidMessage

        private MqttMessage invalidMessage​(java.lang.Throwable cause)
      • decodeFixedHeader

        private static MqttFixedHeader decodeFixedHeader​(ChannelHandlerContext ctx,
                                                         ByteBuf buffer)
        Decodes the fixed header. It's one byte for the flags and then variable bytes for the remaining length.
        Parameters:
        buffer - the buffer to decode from
        Returns:
        the fixed header
      • decodeVariableHeader

        private java.lang.Object decodeVariableHeader​(ChannelHandlerContext ctx,
                                                      ByteBuf buffer,
                                                      MqttFixedHeader mqttFixedHeader)
        Decodes the variable header (if any)
        Parameters:
        buffer - the buffer to decode from
        mqttFixedHeader - MqttFixedHeader of the same message
        Returns:
        the variable header
      • decodeMessageId

        private static int decodeMessageId​(ByteBuf buffer)
        Returns:
        messageId with numberOfBytesConsumed is 2
      • decodePayload

        private java.lang.Object decodePayload​(ChannelHandlerContext ctx,
                                               ByteBuf buffer,
                                               MqttMessageType messageType,
                                               int maxClientIdLength,
                                               java.lang.Object variableHeader)
        Decodes the payload.
        Parameters:
        buffer - the buffer to decode from
        messageType - type of the message being decoded
        variableHeader - variable header of the same message
        Returns:
        the payload
      • decodePublishPayload

        private ByteBuf decodePublishPayload​(ByteBuf buffer)
      • validateNoBytesRemain

        private void validateNoBytesRemain​(int numberOfBytesConsumed)
      • readStrictUtf8

        private java.lang.String readStrictUtf8​(ByteBuf buffer,
                                                int length)
        Reads length bytes from buffer and decodes them as a strictly validated UTF-8 Encoded String per MQTT 3.1.1 and MQTT 5.0. Throws a DecoderException if the sequence is malformed or contains U+0000.
      • decodeByteArray

        private static byte[] decodeByteArray​(ByteBuf buffer)
        Returns:
        the decoded byte[], numberOfBytesConsumed = byte[].length + 2
      • packInts

        private static long packInts​(int a,
                                     int b)
      • unpackA

        private static int unpackA​(long ints)
      • unpackB

        private static int unpackB​(long ints)
      • decodeMsbLsb

        private static int decodeMsbLsb​(ByteBuf buffer)
        numberOfBytesConsumed = 2. return decoded result.
      • decodeVariableByteInteger

        private static long decodeVariableByteInteger​(ByteBuf buffer)
        See 1.5.5 Variable Byte Integer section of MQTT 5.0 specification for encoding/decoding rules
        Parameters:
        buffer - the buffer to decode from
        Returns:
        result pack with a = decoded integer, b = numberOfBytesConsumed. Need to unpack to read them.
        Throws:
        DecoderException - if bad MQTT protocol limits Remaining Length