implemented variable length field

This commit is contained in:
Ziver Koc 2016-03-08 23:27:15 +01:00
parent 6e49a343f9
commit 227f294ae0
3 changed files with 62 additions and 40 deletions

View file

@ -43,36 +43,44 @@ public class ByteUtil {
};
/**
* Creates a new subbyte from offset and with a length and shifts the data to the left
* Creates a new sub byte from index 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 index 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
* @return a new byte containing a sub byte defined by the index and length
*/
public static byte getShiftedBits(byte data, int offset, int length){
int ret = 0xFF & getBits(data, offset, length);
ret = ret >>> offset+1-length;
public static byte getShiftedBits(byte data, int index, int length){
int ret = 0xFF & getBits(data, index, length);
ret = ret >>> index+1-length;
return (byte) ret;
}
/**
* Creates a new subbyte from offset and with a length
* Creates a new sub byte from index and with a length
*
* @param data is the byte data
* @param offset is the bit index, valid values 0-7
* @param index 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
* @return a new byte containing a sub byte defined by the index and length
*/
public static byte getBits(byte data, int offset, int length){
public static byte getBits(byte data, int index, 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]);
byte ret = (byte) (data & getBitMask(index, length));
return ret;
}
/**
* Returns a byte bitmask
*
* @param index start index of the mask, valid values 0-7
* @param length length of mask from index, valid values 1-8
*/
public static byte getBitMask(int index, int 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");
return (byte) BYTE_MASK[index][length];
}
}

View file

@ -24,6 +24,9 @@
package zutil.parser.binary;
import zutil.ByteUtil;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
@ -39,14 +42,14 @@ public class BinaryStructOutputStream {
private OutputStream out;
private byte rest;
private int restLength;
private int restBitLength; // length from Most Significant Bit
public BinaryStructOutputStream(OutputStream out){
this.out = out;
rest = 0;
restLength = 0;
restBitLength = 0;
}
@ -54,9 +57,13 @@ public class BinaryStructOutputStream {
* Generate a binary byte array from the provided struct.
* The byte array will be left
*/
/* public byte[] serialize(BinaryStruct struct) {
}*/
public static byte[] serialize(BinaryStruct struct) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream out = new BinaryStructOutputStream(buffer);
out.write(struct);
out.flush();
return buffer.toByteArray();
}
/**
* Generate a binary stream from the provided struct and
@ -68,8 +75,19 @@ public class BinaryStructOutputStream {
for (BinaryFieldData field : structDataList){
byte[] data = field.getValue(struct);
for (int i=data.length-1; i>=0; --i) {
out.write(data[i]);
int fieldBitLength = field.getBitLength();
for (int i=data.length-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 &= ByteUtil.getBitMask(7-restBitLength, fieldBitLength);
rest |= b;
restBitLength += fieldBitLength;
if (restBitLength >= 8)
localFlush();
}
}
}
}
@ -79,12 +97,15 @@ public class BinaryStructOutputStream {
* Writes any outstanding data to the stream
*/
public void flush() throws IOException {
if(restLength > 0){
localFlush();
out.flush();
}
private void localFlush() throws IOException {
if(restBitLength > 0){
out.write(0xFF & rest);
rest = 0;
restLength = 0;
restBitLength = 0;
}
out.flush();
}
/**

View file

@ -47,23 +47,23 @@ public class BinaryStructOutputStreamTest {
public int i2 = 2;
};
byte[] data = serialize(struct);
byte[] data = BinaryStructOutputStream.serialize(struct);
assertArrayEquals(new byte[]{0,0,0,1, 0,0,0,2}, data);
}
/*
@Test
public void basicBooleanTest() throws IOException {
BinaryStruct struct = new BinaryStruct() {
@BinaryField(index=1, length=1)
public boolean b1 = true;
public boolean b1 = false;
@BinaryField(index=2, length=1)
public boolean b2 = false;
public boolean b2 = true;
};
byte[] data = serialize(struct);
assertArrayEquals(new byte[]{(byte)0b1000_0000}, data);
byte[] data = BinaryStructOutputStream.serialize(struct);
assertArrayEquals(new byte[]{(byte)0b0100_0000}, data);
}
/*
@Test
public void basicStringTest(){
BinaryTestStruct struct = new BinaryTestStruct() {
@ -79,11 +79,4 @@ public class BinaryStructOutputStreamTest {
struct.assertObj();
}
*/
private byte[] serialize(BinaryStruct struct) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
BinaryStructOutputStream out = new BinaryStructOutputStream(buffer);
out.write(struct);
return buffer.toByteArray();
}
}