Implemented sub byting output data

This commit is contained in:
Ziver Koc 2016-04-15 17:58:19 +02:00
parent 40fbbe1012
commit 3edc220ed8
3 changed files with 29 additions and 6 deletions

View file

@ -4,6 +4,7 @@ package zutil.parser.binary;
import java.lang.reflect.Field;
import java.util.*;
import zutil.ByteUtil;
import zutil.ClassUtil;
import zutil.converter.Converter;
import zutil.parser.binary.BinaryStruct.*;
@ -113,11 +114,18 @@ public class BinaryFieldData {
try {
field.setAccessible(true);
if (field.getType() == Boolean.class || field.getType() == boolean.class)
return new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00) };
return ByteUtil.getBits(
new byte[]{ (byte)(field.getBoolean(obj) ? 0x01 : 0x00)},
getBitLength(obj));
else if (field.getType() == Integer.class || field.getType() == int.class)
return Converter.toBytes(field.getInt(obj));
return ByteUtil.getBits(
Converter.toBytes(field.getInt(obj)),
getBitLength(obj));
else if (field.getType() == String.class)
return ((String)(field.get(obj))).getBytes();
return ByteUtil.getReverseByteOrder(
ByteUtil.getBits(
((String)(field.get(obj))).getBytes(),
getBitLength(obj)));
else
throw new UnsupportedOperationException("Unsupported BinaryStruct field type: "+ getType());
} catch (IllegalAccessException e){

View file

@ -81,7 +81,7 @@ public class BinaryStructOutputStream {
byte[] data = field.getByteValue(struct);
int fieldBitLength = field.getBitLength(struct);
for (int i = (int) Math.ceil(fieldBitLength / 8.0) - 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);