Bugfixes in BinaryStruct and some Tests for MDNS
This commit is contained in:
parent
64a9b4126c
commit
96e7d4489a
9 changed files with 320 additions and 117 deletions
|
|
@ -1,12 +1,15 @@
|
|||
package zutil.parser.binary;
|
||||
|
||||
|
||||
import java.io.InvalidClassException;
|
||||
import java.io.InvalidObjectException;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import zutil.ByteUtil;
|
||||
import zutil.converter.Converter;
|
||||
import zutil.parser.binary.BinaryStruct.BinaryField;
|
||||
|
||||
|
|
@ -24,7 +27,7 @@ public class BinaryFieldData implements Comparable<BinaryFieldData> {
|
|||
protected static List<BinaryFieldData> getStructFieldList(Class<? extends BinaryStruct> clazz){
|
||||
if (!cache.containsKey(clazz)) {
|
||||
ArrayList<BinaryFieldData> list = new ArrayList<>();
|
||||
for (Field field : clazz.getFields()) {
|
||||
for (Field field : clazz.getDeclaredFields()) {
|
||||
if (field.isAnnotationPresent(BinaryField.class))
|
||||
list.add(new BinaryFieldData(field));
|
||||
}
|
||||
|
|
@ -56,15 +59,17 @@ public class BinaryFieldData implements Comparable<BinaryFieldData> {
|
|||
}
|
||||
}
|
||||
|
||||
// TODO: variable length support
|
||||
protected byte[] getValue(Object obj){
|
||||
try {
|
||||
field.setAccessible(true);
|
||||
if (field.getType() == Boolean.class || field.getType() == boolean.class)
|
||||
return new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00) };
|
||||
else if (field.getType() == Integer.class || field.getType() == int.class)
|
||||
return Converter.toBytes(field.getInt(obj));
|
||||
else if (field.getType() == String.class)
|
||||
return ((String)(field.get(obj))).getBytes();
|
||||
else
|
||||
throw new UnsupportedOperationException("Unsupported BinaryStruct field class: "+ field.getClass());
|
||||
} catch (IllegalAccessException e){
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -76,7 +76,7 @@ public class BinaryStructOutputStream {
|
|||
byte[] data = field.getValue(struct);
|
||||
|
||||
int fieldBitLength = field.getBitLength();
|
||||
for (int i=data.length-1; fieldBitLength>0; fieldBitLength-=8, --i) {
|
||||
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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue