Package io.netty.handler.codec.mqtt
Class MqttDecoder
- java.lang.Object
-
- All Implemented Interfaces:
ChannelHandler,ChannelInboundHandler
public final class MqttDecoder extends ReplayingDecoder<MqttDecoder.DecoderState>
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static classMqttDecoder.DecoderStateStates of the decoder.private static classMqttDecoder.Result<T>-
Nested classes/interfaces inherited from class io.netty.handler.codec.ByteToMessageDecoder
ByteToMessageDecoder.Cumulator
-
Nested classes/interfaces inherited from interface io.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private intbytesRemainingInVariablePartprivate intmaxAllowedRemainingBytesprivate intmaxBytesInMessageprivate intmaxClientIdLengthprivate MqttFixedHeadermqttFixedHeaderprivate booleanstrictUtf8Validationprivate java.nio.charset.CharsetDecoderutf8Decoderprivate java.lang.ObjectvariableHeader-
Fields inherited from class io.netty.handler.codec.ByteToMessageDecoder
COMPOSITE_CUMULATOR, MERGE_CUMULATOR
-
-
Constructor Summary
Constructors Constructor Description MqttDecoder()MqttDecoder(int maxBytesInMessage)MqttDecoder(int maxBytesInMessage, int maxClientIdLength)MqttDecoder(int maxBytesInMessage, int maxClientIdLength, boolean strictUtf8Validation)Creates a newMqttDecoder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidcheckMaxMessageLengthRemaining(int maxAllowedRemainingBytes)protected voiddecode(ChannelHandlerContext ctx, ByteBuf buffer, java.util.List<java.lang.Object> out)Decode the from oneByteBufto an other.private byte[]decodeByteArray(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttConnAckVariableHeaderdecodeConnAckVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)private MqttConnectPayloaddecodeConnectionPayload(ByteBuf buffer, int maxClientIdLength, MqttConnectVariableHeader mqttConnectVariableHeader, int maxAllowedRemainingBytes)private MqttConnectVariableHeaderdecodeConnectionVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)private MqttFixedHeaderdecodeFixedHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)Decodes the fixed header.private static intdecodeMessageId(ByteBuf buffer)private MqttMessageIdAndPropertiesVariableHeaderdecodeMessageIdAndPropertiesVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)private static intdecodeMsbLsb(ByteBuf buffer)numberOfBytesConsumed = 2.private java.lang.ObjectdecodePayload(ByteBuf buffer, MqttMessageType messageType, int maxClientIdLength, java.lang.Object variableHeader, int maxAllowedRemainingBytes)Decodes the payload.private MqttDecoder.Result<MqttProperties>decodeProperties(ByteBuf buffer, int maxAllowedRemainingBytes)private ByteBufdecodePublishPayload(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttPublishVariableHeaderdecodePublishVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader, int maxAllowedRemainingBytes)private MqttPubReplyMessageVariableHeaderdecodePubReplyMessage(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttReasonCodeAndPropertiesVariableHeaderdecodeReasonCodeAndPropertiesVariableHeader(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttDecoder.Result<java.lang.String>decodeString(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttDecoder.Result<java.lang.String>decodeString(ByteBuf buffer, int minBytes, int maxBytes, int maxAllowedRemainingBytes)private MqttSubAckPayloaddecodeSubackPayload(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttSubscribePayloaddecodeSubscribePayload(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttUnsubAckPayloaddecodeUnsubAckPayload(ByteBuf buffer, int maxAllowedRemainingBytes)private MqttUnsubscribePayloaddecodeUnsubscribePayload(ByteBuf buffer, int maxAllowedRemainingBytes)private longdecodeVariableByteInteger(ByteBuf buffer, int maxAllowedRemainingBytes)See 1.5.5 Variable Byte Integer section of MQTT 5.0 specification for encoding/decoding rulesprivate java.lang.ObjectdecodeVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader, int maxAllowedRemainingBytes)Decodes the variable header (if any)private MqttMessageinvalidMessage(java.lang.Throwable cause)private static longpackInts(int a, int b)private intparseRemainingLength(ByteBuf buffer, MqttMessageType messageType, int maxAllowedRemainingBytes)private java.lang.StringreadStrictUtf8(ByteBuf buffer, int length)Readslengthbytes frombufferand decodes them as a strictly validated UTF-8 Encoded String per MQTT 3.1.1 and MQTT 5.0.private static intunpackA(long ints)private static intunpackB(long ints)private voidvalidateNoBytesRemain(int numberOfBytesConsumed)-
Methods inherited from class io.netty.handler.codec.ReplayingDecoder
callDecode, checkpoint, checkpoint, state, state
-
Methods inherited from class io.netty.handler.codec.ByteToMessageDecoder
actualReadableBytes, channelInactive, channelRead, channelReadComplete, decodeLast, discardSomeReadBytes, handlerRemoved, handlerRemoved0, internalBuffer, isSingleDecode, setCumulator, setDiscardAfterReads, setSingleDecode, userEventTriggered
-
Methods inherited from class io.netty.channel.ChannelInboundHandlerAdapter
channelActive, channelRegistered, channelUnregistered, channelWritabilityChanged, exceptionCaught
-
Methods inherited from class io.netty.channel.ChannelHandlerAdapter
ensureNotSharable, handlerAdded, 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
handlerAdded
-
-
-
-
Field Detail
-
mqttFixedHeader
private MqttFixedHeader mqttFixedHeader
-
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
-
maxAllowedRemainingBytes
private int maxAllowedRemainingBytes
-
-
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 newMqttDecoder.- Parameters:
maxBytesInMessage- the maximum number of bytes a decoded message may consume.maxClientIdLength- the maximum length of the Client Identifier (CONNECT payload).strictUtf8Validation- iftrue(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. Iffalse, the legacy behaviour is preserved, malformed bytes are silently replaced withU+FFFDand U+0000 is accepted.
-
-
Method Detail
-
decode
protected void decode(ChannelHandlerContext ctx, ByteBuf buffer, java.util.List<java.lang.Object> out) throws java.lang.Exception
Description copied from class:ByteToMessageDecoderDecode the from oneByteBufto an other. This method will be called till either the inputByteBufhas nothing to read when return from this method or till nothing was read from the inputByteBuf.- Specified by:
decodein classByteToMessageDecoder- Parameters:
ctx- theChannelHandlerContextwhich thisByteToMessageDecoderbelongs tobuffer- theByteBuffrom which to read dataout- theListto which decoded messages should be added- Throws:
java.lang.Exception- is thrown if an error occurs
-
invalidMessage
private MqttMessage invalidMessage(java.lang.Throwable cause)
-
checkMaxMessageLengthRemaining
private void checkMaxMessageLengthRemaining(int maxAllowedRemainingBytes)
-
decodeFixedHeader
private MqttFixedHeader decodeFixedHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)
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
-
parseRemainingLength
private int parseRemainingLength(ByteBuf buffer, MqttMessageType messageType, int maxAllowedRemainingBytes)
-
decodeVariableHeader
private java.lang.Object decodeVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader, int maxAllowedRemainingBytes)
Decodes the variable header (if any)- Parameters:
buffer- the buffer to decode frommqttFixedHeader- MqttFixedHeader of the same messagemaxAllowedRemainingBytes- the maximum number of bytes permitted to remain- Returns:
- the variable header
-
decodeConnectionVariableHeader
private MqttConnectVariableHeader decodeConnectionVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeConnAckVariableHeader
private MqttConnAckVariableHeader decodeConnAckVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeMessageIdAndPropertiesVariableHeader
private MqttMessageIdAndPropertiesVariableHeader decodeMessageIdAndPropertiesVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodePubReplyMessage
private MqttPubReplyMessageVariableHeader decodePubReplyMessage(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeReasonCodeAndPropertiesVariableHeader
private MqttReasonCodeAndPropertiesVariableHeader decodeReasonCodeAndPropertiesVariableHeader(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodePublishVariableHeader
private MqttPublishVariableHeader decodePublishVariableHeader(ChannelHandlerContext ctx, ByteBuf buffer, MqttFixedHeader mqttFixedHeader, int maxAllowedRemainingBytes)
-
decodeMessageId
private static int decodeMessageId(ByteBuf buffer)
- Returns:
- messageId with numberOfBytesConsumed is 2
-
decodePayload
private java.lang.Object decodePayload(ByteBuf buffer, MqttMessageType messageType, int maxClientIdLength, java.lang.Object variableHeader, int maxAllowedRemainingBytes)
Decodes the payload.- Parameters:
buffer- the buffer to decode frommessageType- type of the message being decodedvariableHeader- variable header of the same message- Returns:
- the payload
-
decodeConnectionPayload
private MqttConnectPayload decodeConnectionPayload(ByteBuf buffer, int maxClientIdLength, MqttConnectVariableHeader mqttConnectVariableHeader, int maxAllowedRemainingBytes)
-
decodeSubscribePayload
private MqttSubscribePayload decodeSubscribePayload(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeSubackPayload
private MqttSubAckPayload decodeSubackPayload(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeUnsubAckPayload
private MqttUnsubAckPayload decodeUnsubAckPayload(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeUnsubscribePayload
private MqttUnsubscribePayload decodeUnsubscribePayload(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodePublishPayload
private ByteBuf decodePublishPayload(ByteBuf buffer, int maxAllowedRemainingBytes)
-
validateNoBytesRemain
private void validateNoBytesRemain(int numberOfBytesConsumed)
-
decodeString
private MqttDecoder.Result<java.lang.String> decodeString(ByteBuf buffer, int maxAllowedRemainingBytes)
-
decodeString
private MqttDecoder.Result<java.lang.String> decodeString(ByteBuf buffer, int minBytes, int maxBytes, int maxAllowedRemainingBytes)
-
readStrictUtf8
private java.lang.String readStrictUtf8(ByteBuf buffer, int length)
Readslengthbytes frombufferand decodes them as a strictly validated UTF-8 Encoded String per MQTT 3.1.1 and MQTT 5.0. Throws aDecoderExceptionif the sequence is malformed or contains U+0000.
-
decodeByteArray
private byte[] decodeByteArray(ByteBuf buffer, int maxAllowedRemainingBytes)
- 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 long decodeVariableByteInteger(ByteBuf buffer, int maxAllowedRemainingBytes)
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
-
decodeProperties
private MqttDecoder.Result<MqttProperties> decodeProperties(ByteBuf buffer, int maxAllowedRemainingBytes)
-
-