Fixed packet ID incorrect parsing

This commit is contained in:
Ziver Koc 2026-01-06 01:50:10 +01:00
parent 0692112e90
commit 7209d677a4
3 changed files with 33 additions and 8 deletions

View file

@ -1,7 +1,7 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020 Ziver Koc * Copyright (c) 2020-2026 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -167,9 +167,10 @@ public class MqttPacketPublish extends MqttPacketHeader {
public Object read(InputStream in, BinaryFieldData field, Object parentObject) throws IOException { public Object read(InputStream in, BinaryFieldData field, Object parentObject) throws IOException {
MqttPacketPublish publish = (MqttPacketPublish) parentObject; MqttPacketPublish publish = (MqttPacketPublish) parentObject;
if (0 < publish.getFlagQoS()) { if (publish.getFlagQoS() > 0) {
byte[] b = new byte[2]; byte[] b = new byte[2];
in.read(b); b[1] = (byte) in.read();
b[0] = (byte) in.read();
return Converter.toInt(b); return Converter.toInt(b);
} }

View file

@ -1,7 +1,7 @@
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2020-2025 Ziver Koc * Copyright (c) 2020-2026 Ziver Koc
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
@ -423,7 +423,7 @@ public class MqttBrokerTest {
// Setup publish message // Setup publish message
MqttPacketPublish publish = new MqttPacketPublish(); MqttPacketPublish publish = new MqttPacketPublish();
publish.topicName = "test/topic"; publish.topicName = "test/topic";
publish.setFlagQoS(3); publish.setFlagQoS(2);
// Test illegal qos // Test illegal qos
assertThrows(UnsupportedOperationException.class, () -> { assertThrows(UnsupportedOperationException.class, () -> {

View file

@ -1,3 +1,27 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2026 Ziver Koc
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package zutil.net.mqtt.packet; package zutil.net.mqtt.packet;
import org.junit.Test; import org.junit.Test;
@ -172,15 +196,15 @@ public class MqttPacketPublishTest {
0xFF & 2, // length 0xFF & 2, // length
'a', // Topic Name 'a', // Topic Name
'b', // Topic Name 'b', // Topic Name
0b0000_0000, // Packet ID 0b0110_1101, // Packet ID 28140 (0x6D)
0b0000_0101 // Packet ID 0b1110_1100 // Packet ID 28140 (0xEC)
// Payload // Payload
}; };
MqttPacketPublish obj = new MqttPacketPublish(); MqttPacketPublish obj = new MqttPacketPublish();
obj.setFlagQoS(MqttPacketPublish.PUBLISH_QOS_2); obj.setFlagQoS(MqttPacketPublish.PUBLISH_QOS_2);
obj.topicName = "ab"; obj.topicName = "ab";
obj.packetId = 5; obj.packetId = 28140;
ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream binOut = new BinaryStructOutputStream(buffer); BinaryStructOutputStream binOut = new BinaryStructOutputStream(buffer);