Added byte support to BinaryStruct

This commit is contained in:
Ziver Koc 2017-08-04 16:10:41 +02:00
parent 59097efcdb
commit fc1e650002
3 changed files with 55 additions and 16 deletions

View file

@ -95,9 +95,11 @@ public class BinaryFieldData {
try { try {
field.setAccessible(true); field.setAccessible(true);
if (field.getType() == Boolean.class || field.getType() == boolean.class) if (field.getType() == Boolean.class || field.getType() == boolean.class)
field.set(obj, data[0] != 0); field.setBoolean(obj, data[0] != 0);
else if (field.getType() == Byte.class || field.getType() == byte.class)
field.setByte(obj, data[0]);
else if (field.getType() == Integer.class || field.getType() == int.class) else if (field.getType() == Integer.class || field.getType() == int.class)
field.set(obj, Converter.toInt(data)); field.setInt(obj, Converter.toInt(data));
else if (field.getType() == String.class) else if (field.getType() == String.class)
field.set(obj, new String(ByteUtil.getReverseByteOrder(data), StandardCharsets.ISO_8859_1)); field.set(obj, new String(ByteUtil.getReverseByteOrder(data), StandardCharsets.ISO_8859_1));
else else
@ -122,6 +124,10 @@ public class BinaryFieldData {
return ByteUtil.getBits( return ByteUtil.getBits(
new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00)}, new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00)},
getBitLength(obj)); getBitLength(obj));
else if (field.getType() == Byte.class || field.getType() == byte.class)
return ByteUtil.getBits(
new byte[]{field.getByte(obj)},
getBitLength(obj));
else if (field.getType() == Integer.class || field.getType() == int.class) else if (field.getType() == Integer.class || field.getType() == int.class)
return ByteUtil.getBits( return ByteUtil.getBits(
Converter.toBytes(field.getInt(obj)), Converter.toBytes(field.getInt(obj)),

View file

@ -93,6 +93,27 @@ public class BinaryStructInputStreamTest {
struct.assertObj(); struct.assertObj();
} }
@Test
public void basicByteTest(){
BinaryTestStruct struct = new BinaryTestStruct() {
@BinaryField(index=1, length=1)
public byte b1;
@BinaryField(index=2, length=4)
public byte b2;
@BinaryField(index=3, length=3)
public byte b3;
public void assertObj(){
assertEquals(0, b1);
assertEquals(6, b2);
assertEquals(6, b3);
}
};
BinaryStructInputStream.read(struct, new byte[]{0b0011_0110});
struct.assertObj();
}
@Test @Test
public void nonLinedLength(){ public void nonLinedLength(){
BinaryTestStruct struct = new BinaryTestStruct() { BinaryTestStruct struct = new BinaryTestStruct() {

View file

@ -38,6 +38,32 @@ import static org.junit.Assert.assertArrayEquals;
*/ */
public class BinaryStructOutputStreamTest { public class BinaryStructOutputStreamTest {
@Test
public void basicBooleanTest() throws IOException {
BinaryStruct struct = new BinaryStruct() {
@BinaryField(index=1, length=1)
public boolean b1 = false;
@BinaryField(index=2, length=1)
public boolean b2 = true;
};
byte[] data = BinaryStructOutputStream.serialize(struct);
assertArrayEquals(new byte[]{(byte)0b0100_0000}, data);
}
@Test
public void basicByteTest() throws IOException {
BinaryStruct struct = new BinaryStruct() {
@BinaryField(index=1, length=3)
public byte b1 = 3;
@BinaryField(index=2, length=5)
public byte b2 = Byte.MAX_VALUE;
};
byte[] data = BinaryStructOutputStream.serialize(struct);
assertArrayEquals(new byte[]{(byte)0b0111_1111}, data);
}
@Test @Test
public void basicIntTest() throws IOException { public void basicIntTest() throws IOException {
BinaryStruct struct = new BinaryStruct() { BinaryStruct struct = new BinaryStruct() {
@ -64,20 +90,6 @@ public class BinaryStructOutputStreamTest {
assertArrayEquals(new byte[]{0,1, 0,2}, data); assertArrayEquals(new byte[]{0,1, 0,2}, data);
} }
@Test
public void basicBooleanTest() throws IOException {
BinaryStruct struct = new BinaryStruct() {
@BinaryField(index=1, length=1)
public boolean b1 = false;
@BinaryField(index=2, length=1)
public boolean b2 = true;
};
byte[] data = BinaryStructOutputStream.serialize(struct);
assertArrayEquals(new byte[]{(byte)0b0100_0000}, data);
}
@Test @Test
public void basicStringTest() throws IOException { public void basicStringTest() throws IOException {
BinaryStruct struct = new BinaryStruct() { BinaryStruct struct = new BinaryStruct() {