some refactoring and bugfixes
This commit is contained in:
parent
4d31e66ebd
commit
63f191db4c
4 changed files with 56 additions and 17 deletions
|
|
@ -6,7 +6,7 @@ package zutil;
|
|||
* Created by Ziver on 2016-01-30.
|
||||
*/
|
||||
public class ByteUtil {
|
||||
|
||||
/** Bitmask array used by utility functions **/
|
||||
private static final int[][] BYTE_MASK = new int[][]{
|
||||
{0b0000_0001},
|
||||
{0b0000_0010, 0b0000_0011},
|
||||
|
|
@ -18,16 +18,37 @@ public class ByteUtil {
|
|||
{0b1000_0000, 0b1100_0000, 0b1110_0000, 0b1111_0000, 0b1111_1000, 0b1111_1100, 0b1111_1110, 0b1111_1111}
|
||||
};
|
||||
|
||||
public static byte getBits(byte data, int index, int length){
|
||||
length--;
|
||||
if(0 > index || index > 7)
|
||||
throw new IllegalArgumentException("Invalid index argument, allowed value is 0-7");
|
||||
if(length < 0 && index-length < 0)
|
||||
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index");
|
||||
|
||||
int ret = data & BYTE_MASK[index][length];
|
||||
ret = ret >>> index-length;
|
||||
/**
|
||||
* Creates a new subbyte from offset and with a length and shifts the data to the left
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param offset is the bit index, valid values 0-7
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a subbyte defined by the offset and length
|
||||
*/
|
||||
public static byte getShiftedBits(byte data, int offset, int length){
|
||||
int ret = 0xFF & getBits(data, offset, length);
|
||||
ret = ret >>> offset+1-length;
|
||||
return (byte) ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new subbyte from offset and with a length
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param offset is the bit index, valid values 0-7
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a subbyte defined by the offset and length
|
||||
*/
|
||||
public static byte getBits(byte data, int offset, int length){
|
||||
length--;
|
||||
if(0 > offset || offset > 7)
|
||||
throw new IllegalArgumentException("Invalid index argument, allowed value is 0-7");
|
||||
if(length < 0 && offset-length < 0)
|
||||
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index");
|
||||
|
||||
byte ret = (byte) (data & BYTE_MASK[offset][length]);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -57,7 +57,7 @@ public class BinaryStructParser {
|
|||
int readLength = 0;
|
||||
byte[] valueData = new byte[(int) Math.ceil(length / 8.0)];
|
||||
for (int index = 0; index < valueData.length; ++index) {
|
||||
valueData[index] = ByteUtil.getBits(data[byteIndex], bitIndex, bitLength);
|
||||
valueData[index] = ByteUtil.getShiftedBits(data[byteIndex], bitIndex, bitLength);
|
||||
readLength += bitLength;
|
||||
byteIndex++;
|
||||
bitIndex = 7;
|
||||
|
|
|
|||
|
|
@ -65,4 +65,22 @@ public class BinaryStructTest {
|
|||
BinaryStructParser.parse(struct, "hello world!".getBytes());
|
||||
struct.assertObj();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void nonLinedLength(){
|
||||
BinaryTestStruct struct = new BinaryTestStruct() {
|
||||
@BinaryField(index=1, length=12)
|
||||
public int i1;
|
||||
@BinaryField(index=2, length=12)
|
||||
public int i2;
|
||||
|
||||
public void assertObj(){
|
||||
assertEquals(17, i1);
|
||||
assertEquals(2048, i2);
|
||||
}
|
||||
};
|
||||
|
||||
BinaryStructParser.parse(struct, new byte[]{0b0000_0001,0b0001_1000,0b0000_0000});
|
||||
struct.assertObj();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,13 +12,13 @@ public class ByteUtilTest {
|
|||
|
||||
|
||||
@Test
|
||||
public void getBits(){
|
||||
assertEquals(1, ByteUtil.getBits((byte)0b1000_0000, 7, 1));
|
||||
assertEquals(1, ByteUtil.getBits((byte)0b0001_0000, 4, 1));
|
||||
assertEquals(1, ByteUtil.getBits((byte)0b0000_0001, 0, 1));
|
||||
public void getShiftedBits(){
|
||||
assertEquals(1, ByteUtil.getShiftedBits((byte)0b1000_0000, 7, 1));
|
||||
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0001_0000, 4, 1));
|
||||
assertEquals(1, ByteUtil.getShiftedBits((byte)0b0000_0001, 0, 1));
|
||||
|
||||
assertEquals(3, ByteUtil.getBits((byte)0b0110_0000, 6, 2));
|
||||
assertEquals(3, ByteUtil.getShiftedBits((byte)0b0110_0000, 6, 2));
|
||||
|
||||
assertEquals((byte)0xFF, ByteUtil.getBits((byte)0b1111_1111, 7, 8));
|
||||
assertEquals((byte)0xFF, ByteUtil.getShiftedBits((byte)0b1111_1111, 7, 8));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue