Fixed BinaryOutputStream bug

This commit is contained in:
Ziver Koc 2016-05-16 23:14:58 +02:00
parent 50ea517cf3
commit c894ba8a61
7 changed files with 164 additions and 57 deletions

View file

@ -78,7 +78,7 @@ public class BinaryStructInputStream {
else {
byte[] valueData = new byte[(int) Math.ceil(field.getBitLength(struct) / 8.0)];
int fieldReadLength = 0; // How much we have read so far
int shiftBy = shiftBy(dataBitIndex, field.getBitLength(struct));
int shiftBy = shiftLeftBy(dataBitIndex, field.getBitLength(struct));
// Parse value
for (int valueDataIndex=valueData.length-1; valueDataIndex >= 0 ; --valueDataIndex) {
@ -101,7 +101,7 @@ public class BinaryStructInputStream {
return totalReadLength;
}
protected static int shiftBy(int bitIndex, int bitLength){
protected static int shiftLeftBy(int bitIndex, int bitLength){
int shiftBy = (8 - ((7-bitIndex) + bitLength) % 8) % 8;
return shiftBy;
}

View file

@ -78,15 +78,16 @@ public class BinaryStructOutputStream {
field.getSerializer().write(out, field.getValue(struct), field);
}
else{
byte[] data = field.getByteValue(struct);
int fieldBitLength = field.getBitLength(struct);
byte[] data = field.getByteValue(struct);
data = ByteUtil.shiftRight(data, ((8 - fieldBitLength % 8) % 8));
for (int i=(int)Math.ceil(fieldBitLength/8.0)-1; fieldBitLength>0; fieldBitLength-=8, --i) {
byte b = data[i];
if (restBitLength == 0 && fieldBitLength >= 8)
out.write(0xFF & b);
else {
b <<= 8 - restBitLength - fieldBitLength;
b = (byte)((b&0xFF) >> restBitLength);
b &= ByteUtil.getBitMask(7 - restBitLength, fieldBitLength);
rest |= b;
restBitLength += fieldBitLength;