Implementation of Variable length BinaryStruct fields, one TC is failing
This commit is contained in:
parent
e41fe70951
commit
dd1b55106b
8 changed files with 139 additions and 21 deletions
|
|
@ -73,13 +73,13 @@ public class ByteUtil {
|
|||
* 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
|
||||
* @param length length of mask from index, valid values 1-8 depending on index
|
||||
*/
|
||||
public static byte getBitMask(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)
|
||||
if(length < 0 || 7-index-length < 0)
|
||||
throw new IllegalArgumentException("Invalid length argument: "+length+", allowed values 1-8 depending on index");
|
||||
return (byte) BYTE_MASK[index][length];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -81,6 +81,34 @@ public class ClassUtil {
|
|||
return primitives.contains( type );
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the given class is a type representing a number without any decimals.
|
||||
* E.g. long, int, short, char, byte and corresponding wrapper.
|
||||
*/
|
||||
public static boolean isNumber(Class<?> type){
|
||||
return Long.class.isAssignableFrom(type) ||
|
||||
long.class.isAssignableFrom(type) ||
|
||||
Integer.class.isAssignableFrom(type) ||
|
||||
int.class.isAssignableFrom(type) ||
|
||||
Short.class.isAssignableFrom(type) ||
|
||||
short.class.isAssignableFrom(type) ||
|
||||
Character.class.isAssignableFrom(type) ||
|
||||
char.class.isAssignableFrom(type) ||
|
||||
Byte.class.isAssignableFrom(type) ||
|
||||
byte.class.isAssignableFrom(type);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return true if the given class is a type representing a number with decimals.
|
||||
* E.g. double, float and corresponding wrapper.
|
||||
*/
|
||||
public static boolean isDecimal(Class<?> type){
|
||||
return Double.class.isAssignableFrom(type) ||
|
||||
double.class.isAssignableFrom(type) ||
|
||||
Float.class.isAssignableFrom(type) ||
|
||||
float.class.isAssignableFrom(type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param field is the field to return the generics from
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ package zutil.parser.binary;
|
|||
import java.lang.reflect.Field;
|
||||
import java.util.*;
|
||||
|
||||
import zutil.ClassUtil;
|
||||
import zutil.converter.Converter;
|
||||
import zutil.parser.binary.BinaryStruct.*;
|
||||
|
||||
|
|
@ -15,8 +16,12 @@ public class BinaryFieldData {
|
|||
|
||||
private int index;
|
||||
private int length;
|
||||
private BinaryFieldSerializer serializer;
|
||||
private Field field;
|
||||
/* @VariableLengthBinaryField */
|
||||
private BinaryFieldData lengthField;
|
||||
private int lengthMultiplier;
|
||||
/* @CustomBinaryField */
|
||||
private BinaryFieldSerializer serializer;
|
||||
|
||||
|
||||
protected static List<BinaryFieldData> getStructFieldList(Class<? extends BinaryStruct> clazz){
|
||||
|
|
@ -25,10 +30,10 @@ public class BinaryFieldData {
|
|||
ArrayList<BinaryFieldData> list = new ArrayList<>();
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(BinaryField.class) ||
|
||||
field.isAnnotationPresent(CustomBinaryField.class))
|
||||
field.isAnnotationPresent(CustomBinaryField.class) ||
|
||||
field.isAnnotationPresent(VariableLengthBinaryField.class))
|
||||
|
||||
list.add(new BinaryFieldData(field));
|
||||
|
||||
}
|
||||
Collections.sort(list, new Comparator<BinaryFieldData>(){
|
||||
@Override
|
||||
|
|
@ -45,21 +50,42 @@ public class BinaryFieldData {
|
|||
}
|
||||
|
||||
|
||||
private BinaryFieldData(Field f) throws IllegalAccessException, InstantiationException {
|
||||
private BinaryFieldData(Field f) throws IllegalAccessException, InstantiationException, NoSuchFieldException {
|
||||
field = f;
|
||||
this.length = -1;
|
||||
this.lengthField = null;
|
||||
this.lengthMultiplier = 1;
|
||||
this.serializer = null;
|
||||
if (field.isAnnotationPresent(CustomBinaryField.class)){
|
||||
CustomBinaryField fieldData = field.getAnnotation(CustomBinaryField.class);
|
||||
index = fieldData.index();
|
||||
serializer = (BinaryFieldSerializer) fieldData.serializer().newInstance();
|
||||
this.index = fieldData.index();
|
||||
this.serializer = fieldData.serializer().newInstance();
|
||||
}
|
||||
else if (field.isAnnotationPresent(VariableLengthBinaryField.class)) {
|
||||
VariableLengthBinaryField fieldData = field.getAnnotation(VariableLengthBinaryField.class);
|
||||
this.index = fieldData.index();
|
||||
this.lengthMultiplier = fieldData.multiplier();
|
||||
this.lengthField = new BinaryFieldData(
|
||||
field.getDeclaringClass().getDeclaredField(fieldData.lengthField()));
|
||||
if ( !ClassUtil.isNumber(lengthField.getType()))
|
||||
throw new IllegalArgumentException("Length variable for VariableLengthBinaryStruct needs to be of a number type.");
|
||||
}
|
||||
else {
|
||||
BinaryField fieldData = field.getAnnotation(BinaryField.class);
|
||||
index = fieldData.index();
|
||||
length = fieldData.length();
|
||||
this.index = fieldData.index();
|
||||
this.length = fieldData.length();
|
||||
}
|
||||
}
|
||||
|
||||
protected void setByteValue(Object obj, byte[] data){
|
||||
|
||||
public String getName(){
|
||||
return field.getName();
|
||||
}
|
||||
public Class<?> getType(){
|
||||
return field.getType();
|
||||
}
|
||||
|
||||
public void setByteValue(Object obj, byte[] data){
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
if (field.getType() == Boolean.class || field.getType() == boolean.class)
|
||||
|
|
@ -74,7 +100,7 @@ public class BinaryFieldData {
|
|||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
protected void setValue(Object obj, Object value){
|
||||
public void setValue(Object obj, Object value){
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
field.set(obj, value);
|
||||
|
|
@ -83,7 +109,7 @@ public class BinaryFieldData {
|
|||
}
|
||||
}
|
||||
|
||||
protected byte[] getByteValue(Object obj){
|
||||
public byte[] getByteValue(Object obj){
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
if (field.getType() == Boolean.class || field.getType() == boolean.class)
|
||||
|
|
@ -93,13 +119,13 @@ public class BinaryFieldData {
|
|||
else if (field.getType() == String.class)
|
||||
return ((String)(field.get(obj))).getBytes();
|
||||
else
|
||||
throw new UnsupportedOperationException("Unsupported BinaryStruct field class: "+ field.getClass());
|
||||
throw new UnsupportedOperationException("Unsupported BinaryStruct field type: "+ getType());
|
||||
} catch (IllegalAccessException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
protected Object getValue(Object obj){
|
||||
public Object getValue(Object obj){
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
return field.get(obj);
|
||||
|
|
@ -110,7 +136,9 @@ public class BinaryFieldData {
|
|||
}
|
||||
|
||||
|
||||
public int getBitLength(){
|
||||
public int getBitLength(Object obj){
|
||||
if(lengthField != null)
|
||||
return (int) lengthField.getValue(obj) * lengthMultiplier;
|
||||
return length;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -34,17 +34,42 @@ import java.lang.annotation.Target;
|
|||
*/
|
||||
public interface BinaryStruct {
|
||||
|
||||
/**
|
||||
* Basic BinaryField with a constant length.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface BinaryField{
|
||||
/** Will be used to order the fields are read. Lowest index number field will be read first. */
|
||||
int index();
|
||||
/** Defines the bit length of the data */
|
||||
int length();
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used for fields that are of variable length.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface VariableLengthBinaryField{
|
||||
/** Will be used to order the fields are read. Lowest index number field will be read first. */
|
||||
int index();
|
||||
/** The name of the field that will contain the length of the data to read. */
|
||||
String lengthField();
|
||||
/** Defines the multiplier used on the lengthField to convert to length in bits which is used internally.
|
||||
* Default value is 8. */
|
||||
int multiplier() default 8;
|
||||
}
|
||||
|
||||
/**
|
||||
* Can be used with fields that need a custom serializer.
|
||||
*/
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.FIELD)
|
||||
@interface CustomBinaryField{
|
||||
/** Will be used to order the fields are read. Lowest index number field will be read first. */
|
||||
int index();
|
||||
Class serializer();
|
||||
/** Defines the serializer class that will be used. Class needs to be publicly visible. */
|
||||
Class<? extends BinaryFieldSerializer> serializer();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ public class BinaryStructInputStream {
|
|||
field.setValue(struct, value);
|
||||
}
|
||||
else {
|
||||
byte[] valueData = new byte[(int) Math.ceil(field.getBitLength() / 8.0)];
|
||||
byte[] valueData = new byte[(int) Math.ceil(field.getBitLength(struct) / 8.0)];
|
||||
int fieldReadLength = 0;
|
||||
|
||||
// Parse value
|
||||
|
|
@ -85,7 +85,7 @@ public class BinaryStructInputStream {
|
|||
data = (byte) in.read();
|
||||
dataBitIndex = 7;
|
||||
}
|
||||
int bitLength = Math.min(dataBitIndex + 1, field.getBitLength() - fieldReadLength);
|
||||
int bitLength = Math.min(dataBitIndex + 1, field.getBitLength(struct) - fieldReadLength);
|
||||
valueData[valueDataIndex] = ByteUtil.getShiftedBits(data, dataBitIndex, bitLength);
|
||||
fieldReadLength += bitLength;
|
||||
dataBitIndex -= bitLength;
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ public class BinaryStructOutputStream {
|
|||
else{
|
||||
byte[] data = field.getByteValue(struct);
|
||||
|
||||
int fieldBitLength = field.getBitLength();
|
||||
int fieldBitLength = field.getBitLength(struct);
|
||||
for (int i = (int) Math.ceil(fieldBitLength / 8.0) - 1; fieldBitLength > 0; fieldBitLength -= 8, --i) {
|
||||
byte b = data[i];
|
||||
if (restBitLength == 0 && fieldBitLength >= 8)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue