Fixed BinaryOutputStream bug
This commit is contained in:
parent
50ea517cf3
commit
c894ba8a61
7 changed files with 164 additions and 57 deletions
|
|
@ -44,16 +44,6 @@ public class ByteUtil {
|
|||
{0b1000_0000, 0b1100_0000, 0b1110_0000, 0b1111_0000, 0b1111_1000, 0b1111_1100, 0b1111_1110, 0b1111_1111}
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a new sub byte from MSB to the given length
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a sub byte defined by the index and length
|
||||
*/
|
||||
public static byte getBits(byte data, int length){
|
||||
return getBits(data, length-1, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sub byte from index and with the given length length
|
||||
|
|
@ -82,6 +72,17 @@ public class ByteUtil {
|
|||
return (byte) ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sub byte from LSB to the given length
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a sub byte defined by the index and length
|
||||
*/
|
||||
public static byte getBits(byte data, int length){
|
||||
return getBits(data, length-1, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sub byte array with only the given length of bits from the LSB.
|
||||
*
|
||||
|
|
@ -97,6 +98,17 @@ public class ByteUtil {
|
|||
return dest;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new sub byte from MSB to the given length
|
||||
*
|
||||
* @param data is the byte data
|
||||
* @param length is the length of bits to return, valid values 1-8
|
||||
* @return a new byte containing a sub byte defined by the index and length
|
||||
*/
|
||||
public static byte getBitsMSB(byte data, int length){
|
||||
return getShiftedBits(data, 7, length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new byte array with reversed byte ordering
|
||||
* (LittleEndian -> BigEndian, BigEndian -> LittleEndian)
|
||||
|
|
@ -144,7 +156,7 @@ public class ByteUtil {
|
|||
byte rest = 0;
|
||||
for (int i=0; i<data.length; ++i){
|
||||
rest = (byte)(getBits(data[i], shiftBy-1, shiftBy) << 8 - shiftBy);
|
||||
data[i] = (byte)((int)(data[i]&0xFF) >> shiftBy);
|
||||
data[i] = (byte)((data[i]&0xFF) >>> shiftBy);
|
||||
if(i != 0)
|
||||
data[i-1] |= rest;
|
||||
}
|
||||
|
|
@ -152,6 +164,31 @@ public class ByteUtil {
|
|||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Shifts a whole byte array to the right by the specified amount.
|
||||
*
|
||||
* @param data the array to be shifted
|
||||
* @param shiftBy the amount to shift. Currently only supports maximum value of 8
|
||||
* @return same data reference as the data input
|
||||
*/
|
||||
public static byte[] shiftRight(byte[] data, int shiftBy) {
|
||||
if(0 > shiftBy || shiftBy > 8)
|
||||
throw new IllegalArgumentException("Invalid shiftBy("+shiftBy+") argument, allowed values: 0-8");
|
||||
if (shiftBy == 0)
|
||||
return data;
|
||||
|
||||
byte rest = 0;
|
||||
for (int i=0; i<data.length; ++i){
|
||||
byte preRest = getBitsMSB(data[i], shiftBy);
|
||||
data[i] = (byte)(data[i] << shiftBy);
|
||||
if(i != 0)
|
||||
data[i] |= rest;
|
||||
rest = preRest;
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Presents a binary array in HEX and ASCII
|
||||
|
|
|
|||
|
|
@ -24,6 +24,7 @@
|
|||
|
||||
package zutil.converter;
|
||||
|
||||
import zutil.ByteUtil;
|
||||
import zutil.io.DynamicByteArrayStream;
|
||||
import zutil.parser.Base64Decoder;
|
||||
|
||||
|
|
@ -186,6 +187,21 @@ public class Converter {
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Converts a byte to a Bit String (e.g 01100100)
|
||||
*
|
||||
* @param raw the byte array to convert
|
||||
* @return a bit String
|
||||
*/
|
||||
public static String toBitString(byte raw){
|
||||
StringBuilder str = new StringBuilder(8);
|
||||
for (int i=0; i<8; ++i) {
|
||||
str.append(raw & 0x01);
|
||||
raw >>>= 1;
|
||||
}
|
||||
return str.reverse().toString();
|
||||
}
|
||||
|
||||
/** array needed for byteToHex */
|
||||
private static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue