BiteUtil makes more sense now as index is counted from LSB and length towards MSB
This commit is contained in:
parent
c45809048a
commit
3cd3a2fc7c
7 changed files with 148 additions and 67 deletions
|
|
@ -42,23 +42,30 @@ public class ByteUtilTest {
|
|||
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0001_0000, 4, 1));
|
||||
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0000_0001, 0, 1));
|
||||
|
||||
assertEquals(3, ByteUtil.getShiftedBits((byte)0b0110_0000, 6, 2));
|
||||
assertEquals(3, ByteUtil.getShiftedBits((byte)0b0110_0000, 5, 2));
|
||||
|
||||
assertEquals((byte)0xFF, ByteUtil.getShiftedBits((byte)0b1111_1111, 7, 8));
|
||||
assertEquals((byte)0xFF, ByteUtil.getShiftedBits((byte)0b1111_1111, 0, 8));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void getBits() {
|
||||
assertEquals(0x01, ByteUtil.getBits((byte)0x11, 1));
|
||||
assertEquals(0x03, ByteUtil.getBits((byte)0x13, 4));
|
||||
assertEquals((byte)0x55, ByteUtil.getBits((byte)0x55, 8));
|
||||
assertEquals(0b0000_0001, ByteUtil.getBits((byte)0b0001_0001, 1));
|
||||
assertEquals(0b0000_0011, ByteUtil.getBits((byte)0b000_0011, 4));
|
||||
assertEquals((byte)0b0101_0101, ByteUtil.getBits((byte)0b0101_0101, 8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBitMask() {
|
||||
assertEquals((byte)0b0001, ByteUtil.getBitMask(0, 1));
|
||||
assertEquals((byte)0b0110, ByteUtil.getBitMask(1, 2));
|
||||
assertEquals((byte)0b1111_0000, ByteUtil.getBitMask(4, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getBitsMSB() {
|
||||
assertEquals(0x01, ByteUtil.getBitsMSB((byte)0x80, 1));
|
||||
assertEquals(0x05, ByteUtil.getBitsMSB((byte)0x52, 4));
|
||||
assertEquals(0b0000_0101, ByteUtil.getBitsMSB((byte)0b0101_0010, 4));
|
||||
assertEquals((byte)0x55, ByteUtil.getBitsMSB((byte)0x55, 8));
|
||||
assertEquals((byte)0x03, ByteUtil.getBitsMSB((byte)0xFF, 2));
|
||||
assertEquals((byte)0x0F, ByteUtil.getBitsMSB((byte)0xFF, 4));
|
||||
|
|
@ -109,41 +116,45 @@ public class ByteUtilTest {
|
|||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void shiftRight() {
|
||||
assertArrayEquals( new byte[]{},
|
||||
ByteUtil.shiftRight(new byte[]{}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_0001}, 0));
|
||||
assertArrayEquals( new byte[]{0b0000_0000},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_0001}, 1));
|
||||
assertArrayEquals( new byte[]{0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0001_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_1000},
|
||||
ByteUtil.shiftRight(new byte[]{(byte)0b1000_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0001_0001, 0b0000_0000},
|
||||
ByteUtil.shiftRight(new byte[]{0b0001_0000, 0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{0b0100_1001, 0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0111_1111, 0b0101_0010}, 6));
|
||||
assertArrayEquals( new byte[]{0b0000_0001,0b0000_0001,0b0000_0001,0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0001_0000,0b0001_0000,0b0001_0000,0b0001_0000}, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shiftLeft() {
|
||||
assertArrayEquals( new byte[]{},
|
||||
ByteUtil.shiftLeft(new byte[]{}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_0001},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0000_0001}, 0));
|
||||
assertArrayEquals( new byte[]{0b0000_0001},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0001_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_1000},
|
||||
ByteUtil.shiftLeft(new byte[]{(byte)0b1000_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0001_0001, 0b0000_0000},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0001_0000, 0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{0b0100_1001, 0b0000_0001},
|
||||
assertArrayEquals( new byte[]{(byte)0b0000_0010},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0000_0001}, 1));
|
||||
assertArrayEquals( new byte[]{(byte)0b0001_0000},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{(byte)0b1000_0000},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0000_1000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_0000},
|
||||
ByteUtil.shiftLeft(new byte[]{(byte)0b0001_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0001_0000, 0b0001_0000},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0000_0001, 0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{(byte)0b1100_0000, (byte)0b1001_1111},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0111_1111, 0b0101_0010}, 6));
|
||||
assertArrayEquals( new byte[]{0b0000_0001,0b0000_0001,0b0000_0001,0b0000_0001},
|
||||
assertArrayEquals( new byte[]{0b0000_0000,0b0000_0001,0b0000_0001,0b0000_0001},
|
||||
ByteUtil.shiftLeft(new byte[]{0b0001_0000,0b0001_0000,0b0001_0000,0b0001_0000}, 4));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shiftRight() {
|
||||
assertArrayEquals( new byte[]{},
|
||||
ByteUtil.shiftRight(new byte[]{}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_0001}, 0));
|
||||
assertArrayEquals( new byte[]{(byte)0b0001_0000},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{(byte)0b1000_0000},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_1000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0000_0000},
|
||||
ByteUtil.shiftRight(new byte[]{(byte)0b0001_0000}, 4));
|
||||
assertArrayEquals( new byte[]{0b0001_0000, 0b0001_0000},
|
||||
ByteUtil.shiftRight(new byte[]{0b0000_0001, 0b0000_0001}, 4));
|
||||
assertArrayEquals( new byte[]{(byte)0b1100_0000, (byte)0b1001_1111},
|
||||
ByteUtil.shiftRight(new byte[]{0b0111_1111, 0b0101_0010}, 6));
|
||||
assertArrayEquals( new byte[]{0b0000_0000,0b0000_0001,0b0000_0001,0b0000_0001},
|
||||
ByteUtil.shiftRight(new byte[]{0b0001_0000,0b0001_0000,0b0001_0000,0b0001_0000}, 4));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
27
test/zutil/net/mqtt/MqttBrokerRun.java
Normal file
27
test/zutil/net/mqtt/MqttBrokerRun.java
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package zutil.net.mqtt;
|
||||
|
||||
import zutil.log.CompactLogFormatter;
|
||||
import zutil.log.LogUtil;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class MqttBrokerRun implements MqttSubscriptionListener {
|
||||
private static final Logger logger = LogUtil.getLogger();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
LogUtil.setGlobalLevel(Level.FINEST);
|
||||
LogUtil.setGlobalFormatter(new CompactLogFormatter());
|
||||
|
||||
MqttBroker mqttBroker = new MqttBroker();
|
||||
mqttBroker.addGlobalSubscriber(new MqttBrokerRun());
|
||||
mqttBroker.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dataPublished(String topic, byte[] data) {
|
||||
logger.info("MQTT data published(topic: " + topic + "): " + new String(data, StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
|
|
@ -36,6 +36,47 @@ public class MqttPacketPublishTest {
|
|||
assertArrayEquals(new byte[0], obj.payload);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodeFlags() throws IOException {
|
||||
char[] data = new char[]{
|
||||
// Fixed Header
|
||||
0b0011_1000, // Packet Type(4) + Reserved(4)
|
||||
0xFF & 4, // Variable Header + Payload Length
|
||||
// Variable Header
|
||||
0b0000_0000, // length
|
||||
0xFF & 2, // length
|
||||
'a', // Topic Name
|
||||
'b', // Topic Name
|
||||
// Payload
|
||||
};
|
||||
|
||||
MqttPacketPublish obj = (MqttPacketPublish) MqttPacket.read(
|
||||
new BinaryStructInputStream(new ByteArrayInputStream(Converter.toBytes(data))));
|
||||
|
||||
assertTrue(obj.getFlagDup());
|
||||
assertEquals(MqttPacketPublish.PUBLISH_QOS_0, obj.getFlagQoS());
|
||||
assertFalse(obj.getFlagRetain());
|
||||
|
||||
data = new char[]{
|
||||
// Fixed Header
|
||||
0b0011_0101, // Packet Type(4) + Reserved(4)
|
||||
0xFF & 4, // Variable Header + Payload Length
|
||||
// Variable Header
|
||||
0b0000_0000, // length
|
||||
0xFF & 2, // length
|
||||
'a', // Topic Name
|
||||
'b', // Topic Name
|
||||
// Payload
|
||||
};
|
||||
|
||||
obj = (MqttPacketPublish) MqttPacket.read(
|
||||
new BinaryStructInputStream(new ByteArrayInputStream(Converter.toBytes(data))));
|
||||
|
||||
assertFalse(obj.getFlagDup());
|
||||
assertEquals(MqttPacketPublish.PUBLISH_QOS_2, obj.getFlagQoS());
|
||||
assertTrue(obj.getFlagRetain());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decodePayload() throws IOException {
|
||||
char[] data = new char[]{
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ public class BinaryStructOutputStreamTest {
|
|||
};
|
||||
|
||||
byte[] data = BinaryStructOutputStream.serialize(struct);
|
||||
assertArrayEquals(new byte[]{(byte)0b0111_1111}, data);
|
||||
assertArrayEquals(new byte[]{(byte)0b011_11111}, data);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue