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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue