Initial implementation of binary struct
This commit is contained in:
parent
95e6a2fbcf
commit
7466e02fd9
3 changed files with 108 additions and 0 deletions
19
src/zutil/parser/binary/BinaryStruct.java
Executable file
19
src/zutil/parser/binary/BinaryStruct.java
Executable file
|
|
@ -0,0 +1,19 @@
|
||||||
|
package zutil.parser.binary;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ezivkoc on 2016-01-28.
|
||||||
|
*/
|
||||||
|
public interface BinaryStruct {
|
||||||
|
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
@interface BinaryField{
|
||||||
|
int index();
|
||||||
|
int length();
|
||||||
|
}
|
||||||
|
}
|
||||||
54
src/zutil/parser/binary/BinaryStructParser.java
Executable file
54
src/zutil/parser/binary/BinaryStructParser.java
Executable file
|
|
@ -0,0 +1,54 @@
|
||||||
|
package zutil.parser.binary;
|
||||||
|
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import zutil.parser.binary.BinaryStruct.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ezivkoc on 2016-01-28.
|
||||||
|
*/
|
||||||
|
public class BinaryStructParser {
|
||||||
|
|
||||||
|
public static void parse(BinaryStruct struct, byte[] bytes) {
|
||||||
|
List<BinaryFieldData> structDataList = getStructDataList(struct.getClass());
|
||||||
|
int bitIndex;
|
||||||
|
for(BinaryFieldData field : structDataList){
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static List<BinaryFieldData> getStructDataList(Class<? extends BinaryStruct> clazz){
|
||||||
|
ArrayList<BinaryFieldData> list = new ArrayList<>();
|
||||||
|
for (Field field : clazz.getFields()){
|
||||||
|
if (field.isAnnotationPresent(BinaryField.class))
|
||||||
|
list.add(new BinaryFieldData(field));
|
||||||
|
}
|
||||||
|
Collections.sort(list);
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public static class BinaryFieldData implements Comparable<BinaryFieldData> {
|
||||||
|
private int index;
|
||||||
|
private int length;
|
||||||
|
private Field field;
|
||||||
|
|
||||||
|
protected BinaryFieldData(Field f){
|
||||||
|
field = f;
|
||||||
|
BinaryField fieldData = field.getAnnotation(BinaryField.class);
|
||||||
|
index = fieldData.index();
|
||||||
|
length = fieldData.index();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int compareTo(BinaryFieldData o) {
|
||||||
|
return this.index - o.index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
35
test/zutil/test/BinaryStructTest.java
Executable file
35
test/zutil/test/BinaryStructTest.java
Executable file
|
|
@ -0,0 +1,35 @@
|
||||||
|
package zutil.test;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import zutil.parser.binary.BinaryStruct;
|
||||||
|
import zutil.parser.binary.BinaryStructParser;
|
||||||
|
|
||||||
|
import static junit.framework.Assert.assertEquals;
|
||||||
|
import static junit.framework.TestCase.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by ezivkoc on 2016-01-28.
|
||||||
|
*/
|
||||||
|
public class BinaryStructTest {
|
||||||
|
interface BinaryTestStruct extends BinaryStruct{
|
||||||
|
void assertObj();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void basicTest(){
|
||||||
|
BinaryTestStruct struct = new BinaryTestStruct() {
|
||||||
|
@BinaryField(index=1, length=32)
|
||||||
|
public int i1;
|
||||||
|
@BinaryField(index=2, length=32)
|
||||||
|
public int i2;
|
||||||
|
|
||||||
|
public void assertObj(){
|
||||||
|
assertEquals(1, i1);
|
||||||
|
assertEquals(2, i2);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
BinaryStructParser.parse(struct, new byte[]{0,0,0,1, 0,0,0,2});
|
||||||
|
struct.assertObj();
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue