Bugfix in variable int encoding for MQTT

This commit is contained in:
Ziver Koc 2024-10-03 23:12:05 +02:00
parent dd6ef1b3b7
commit bb3f92fe72
4 changed files with 140 additions and 5 deletions

View file

@ -89,15 +89,14 @@ public class MqttPacketPublish extends MqttPacketHeader {
// ------------------------------------------
// - Application data
@CustomBinaryField(index = 3000, serializer = MqttPacketPublishPayloadSerializer.class)
public byte[] payload;
@Override
public int calculatePayloadLength() {
return payload == null ? 0 : payload.length;
}
@CustomBinaryField(index = 3000, serializer = MqttPacketPublishPayloadSerializer.class)
public byte[] payload;
// ------------------------------------------
// Util methods
// ------------------------------------------

View file

@ -33,6 +33,7 @@ import java.io.OutputStream;
/**
* Code from MQTT specification
* @see <a href="http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180836">2.2.3 Remaining Length (variable length encoding scheme)</a>
*/
public class MqttVariableIntSerializer implements BinaryFieldSerializer<Integer> {
@ -60,7 +61,7 @@ public class MqttVariableIntSerializer implements BinaryFieldSerializer<Integer>
x = x / 128;
// if there are more data to encode, set the top bit of this byte
if (x > 0)
encodedByte = encodedByte & 128;
encodedByte = encodedByte | 128;
out.write(encodedByte);
} while (x > 0);
}