- 插件 JavaStruct, 下载地址
代码示例
import struct.StructClass;
import struct.StructField;
@StructClass
public class Head {
@StructField(order = 0)
private short flag1; //2
private short flagLable;
@StructField(order = 1)
private byte flag2; //1
@StructField(order = 2)
private int flag3; //4
@StructField(order = 3)
private long flag4; //8
@StructField(order = 4)
private byte[] flag5 = new byte[16]; //16
@StructField(order = 5)
private byte flag6;
public byte getFlag6() {
return flag6;
}
public void setFlag6(byte flag6) {
this.flag6 = flag6;
}
public Head(){}
public short getFlag1() {
return flag1;
}
public void setFlag1(short flag1) {
this.flag1 = flag1;
}
public short getFlagLable() {
return flagLable;
}
public void setFlagLable(short flagLable) {
this.flagLable = flagLable;
}
public byte getFlag2() {
return flag2;
}
public void setFlag2(byte flag2) {
this.flag2 = flag2;
}
public int getFlag3() {
return flag3;
}
public void setFlag3(int flag3) {
this.flag3 = flag3;
}
public long getFlag4() {
return flag4;
}
public void setFlag4(long flag4) {
this.flag4 = flag4;
}
public byte[] getFlag5() {
return flag5;
}
public void setFlag5(byte[] flag5) {
this.flag5 = flag5;
}
public static void main(String[] args) throws StructException {
byte []data = {
1, 2, //4
3, //1
1, 1, 1, 1, //4
1, 2, 1, 2, 1, 2, 1, 2, //8
2, 1, 2, 1, 1, 2, 1, 3, //16
1, 1, 2, 1, 1, 2, 1, 3,
4, //1
1, 1, 2, 1, 1, 2, 1, 3,
1, 1, 2, 1, 1, 2, 1, 3,
}; //1024 字节, 1字节 = 8bit
System.out.println("Test byte[] arry len is: " + data.length);
Head head = new Head();
JavaStruct.unpack(head, data);
System.out.println(head.getFlag1());
System.out.println(head.getFlag2());
System.out.println(head.getFlag3());
System.out.println(head.getFlag4());
System.out.println(head.getFlag5());
System.out.println(head.getFlag6());
}
}
maven 中添加本地依赖
<dependency>
<groupId>funcool</groupId>
<artifactId>struct</artifactId>
<version>0.1</version> <!--这里测试0.1 ok-->
<scope>system</scope>
<systemPath>${project.basedir}/lib/javastruct-0.1.jar</systemPath>
</dependency>