Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class MqttCodec
- Object
- MqttCodec
Encodes and decodes MQTT 3.1.1 control packets on plain streams, so it is
independent of the transport. QoS 2 packets (PUBREC, PUBREL, PUBCOMP) and a
PUBLISH at QoS 2 are rejected with an
MqttException.Fields
public static final int MAX_REMAINING_LENGTH = 268435455 | The largest remaining length the protocol can express, 268435455. |
Methods
public static byte[] encodeRemainingLength(int value) | Encodes a remaining length as the protocol’s variable length integer. |
public static byte[] encode(MqttPacket p)
throws IOException | Encodes a packet to its wire form. |
public static void write(MqttPacket p, OutputStream out)
throws IOException | Encodes a packet and writes it to a stream, without flushing it. |
public static int packetLength(byte[] buf, int off, int len, int maxRemaining)
throws IOException | Measures the first packet in a buffer. |
public static MqttPacket read(InputStream in, int maxRemaining)
throws IOException | Reads one packet from a stream, blocking until it is complete. |
public static MqttPacket decode(int first, byte[] body)
throws IOException | Decodes a packet from its first byte and its body. |
Inherited methods
Field details
MAX_REMAINING_LENGTH
public static final int MAX_REMAINING_LENGTH = 268435455The largest remaining length the protocol can express, 268435455.
Method details
encodeRemainingLength
public static byte[] encodeRemainingLength(int value)Encodes a remaining length as the protocol’s variable length integer.
Parameters
valueint- 0 to 268435455
Returns
one to four bytes
encode
public static byte[] encode(MqttPacket p)
throws IOExceptionEncodes a packet to its wire form.
Throws
MqttException when the packet cannot be represented
Parameters
pMqttPacket- the packet
Returns
the fixed header followed by the variable header and payload
Throws
write
public static void write(MqttPacket p, OutputStream out)
throws IOExceptionEncodes a packet and writes it to a stream, without flushing it.
Parameters
pMqttPacket- the packet
outOutputStream- the destination
Throws
packetLength
public static int packetLength(byte[] buf, int off, int len, int maxRemaining)
throws IOExceptionMeasures the first packet in a buffer.
Throws
MqttException when the length is malformed or above the limit
Parameters
bufbyte[]- received bytes
offint- where the packet starts
lenint- how many bytes are available from
off maxRemainingint- the largest remaining length to accept
Returns
the total size of the packet, fixed header included, or -1 when more
bytes are needed
Throws
read
public static MqttPacket read(InputStream in, int maxRemaining)
throws IOExceptionReads one packet from a stream, blocking until it is complete.
Throws
EOFException when the stream ends, MqttException for a malformed or
oversized packet
Parameters
inInputStream- the source
maxRemainingint- the largest remaining length to accept
Returns
the packet
Throws
decode
public static MqttPacket decode(int first, byte[] body)
throws IOExceptionDecodes a packet from its first byte and its body.
Parameters
firstint- the fixed header’s first byte (type and flags)
bodybyte[]- the bytes after the remaining length
Returns
the packet