Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class MqttCodec

  1. Object
  2. 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 = 268435455The 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 IOExceptionEncodes a packet to its wire form.
public static void write(MqttPacket p, OutputStream out) throws IOExceptionEncodes a packet and writes it to a stream, without flushing it.
public static int packetLength(byte[] buf, int off, int len, int maxRemaining) throws IOExceptionMeasures the first packet in a buffer.
public static MqttPacket read(InputStream in, int maxRemaining) throws IOExceptionReads one packet from a stream, blocking until it is complete.
public static MqttPacket decode(int first, byte[] body) throws IOExceptionDecodes a packet from its first byte and its body.

Inherited methods

Field details

MAX_REMAINING_LENGTH

public static final int MAX_REMAINING_LENGTH = 268435455
The 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

value int
0 to 268435455

Returns

one to four bytes

encode

public static byte[] encode(MqttPacket p) throws IOException

Encodes a packet to its wire form.

Throws

MqttException when the packet cannot be represented

Parameters

p MqttPacket
the packet

Returns

the fixed header followed by the variable header and payload

write

public static void write(MqttPacket p, OutputStream out) throws IOException
Encodes a packet and writes it to a stream, without flushing it.

Parameters

p MqttPacket
the packet
out OutputStream
the destination

packetLength

public static int packetLength(byte[] buf, int off, int len, int maxRemaining) throws IOException

Measures the first packet in a buffer.

Throws

MqttException when the length is malformed or above the limit

Parameters

buf byte[]
received bytes
off int
where the packet starts
len int
how many bytes are available from off
maxRemaining int
the largest remaining length to accept

Returns

the total size of the packet, fixed header included, or -1 when more bytes are needed

read

public static MqttPacket read(InputStream in, int maxRemaining) throws IOException

Reads one packet from a stream, blocking until it is complete.

Throws

EOFException when the stream ends, MqttException for a malformed or oversized packet

Parameters

in InputStream
the source
maxRemaining int
the largest remaining length to accept

Returns

the packet

decode

public static MqttPacket decode(int first, byte[] body) throws IOException
Decodes a packet from its first byte and its body.

Parameters

first int
the fixed header’s first byte (type and flags)
body byte[]
the bytes after the remaining length

Returns

the packet