// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import dk.vajhoej.record.Alignment;
import dk.vajhoej.record.Endian;
import dk.vajhoej.record.FieldType;
import dk.vajhoej.record.RecordException;
import dk.vajhoej.record.Struct;
import dk.vajhoej.record.StructField;
import dk.vajhoej.record.StructReader;
import dk.vajhoej.record.StructWriter;

public class StructSer {
    @Struct(alignment=Alignment.PACKED, endianess=Endian.LITTLE)
    public static class YourStruct {
        @StructField(n=0, type=FieldType.UINT1)
        private int field1;
        @StructField(n=1, type=FieldType.UINT2)
        private int field2;
        public YourStruct() {
            this(0, 0);
        }
        public YourStruct(int field1, int field2) {
            this.field1 = field1;
            this.field2 = field2;
        }
        public int getField1() {
            return field1;
        }
        public int getField2() {
            return field2;
        }
    }
    public static void main(String[] args) throws RecordException {
        YourStruct o = new YourStruct(2, 257);
        StructWriter sw = new StructWriter();
        sw.write(o);
        byte[] b = sw.getBytes();
        for(byte b1 : b) {
            System.out.println(b1);
        }
        StructReader sr = new StructReader(b);
        YourStruct o2 = sr.read(YourStruct.class);
        System.out.println(o2.getField1() + " " + o2.getField2());
    }
}
