Impl variable length binary struct. Data will now be shifted to the correct possitions

This commit is contained in:
Ziver Koc 2016-05-11 17:11:18 +02:00
parent bae988e8dd
commit 3f21caa35b
7 changed files with 77 additions and 45 deletions

View file

@ -95,7 +95,7 @@ public class BinaryFieldData {
else if (field.getType() == Integer.class || field.getType() == int.class)
field.set(obj, Converter.toInt(data));
else if (field.getType() == String.class)
field.set(obj, new String(data, StandardCharsets.ISO_8859_1));
field.set(obj, new String(ByteUtil.getReverseByteOrder(data), StandardCharsets.ISO_8859_1));
else
throw new UnsupportedOperationException("Unsupported BinaryStruct field class: "+ field.getClass());
} catch (IllegalAccessException e){

View file

@ -77,20 +77,23 @@ public class BinaryStructInputStream {
}
else {
byte[] valueData = new byte[(int) Math.ceil(field.getBitLength(struct) / 8.0)];
int fieldReadLength = 0;
int fieldReadLength = 0; // How much we have read so far
int shiftBy = (dataBitIndex+1 + field.getBitLength(struct)) % 8;
// Parse value
for (int valueDataIndex = 0; valueDataIndex < valueData.length; ++valueDataIndex) {
for (int valueDataIndex=valueData.length-1; valueDataIndex >= 0 ; --valueDataIndex) {
if (dataBitIndex < 0) { // Read new data?
data = (byte) in.read();
dataBitIndex = 7;
}
int bitLength = Math.min(dataBitIndex + 1, field.getBitLength(struct) - fieldReadLength);
valueData[valueDataIndex] = ByteUtil.getShiftedBits(data, dataBitIndex, bitLength);
fieldReadLength += bitLength;
dataBitIndex -= bitLength;
int subBitLength = Math.min(dataBitIndex + 1, field.getBitLength(struct) - fieldReadLength);
valueData[valueDataIndex] = ByteUtil.getBits(data, dataBitIndex, subBitLength);
fieldReadLength += subBitLength;
dataBitIndex -= subBitLength;
}
// Set value
ByteUtil.shiftLeft(valueData, shiftBy); // shift data so that LSB is at the beginning
field.setByteValue(struct, valueData);
totalReadLength += fieldReadLength;
}