• java字节流转对象,应用于协议解析


    1. 插件 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>
    
  • 相关阅读:
    修改silverlight DataGrid当前选中行及选中列的背景色
    转 Introduction to SharePoint Feature Stapling – Part 2
    File.ReadAllText方法,File.WriteAllText方法修改文件内容
    用xlst将xml转换html
    简单在线编辑器<转>
    随机数生成
    asp.net 2.0 的代码隐藏
    Microsoft .NET Pet Shop 3.x: .NET Pet Shop 的设计模式与体系结构
    并发术语解释
    如何在Render方法中修改替换HtmlTextWriter中的内容
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/15451488.html
Copyright © 2020-2023  润新知