How to parse/encode binary message formats?

前端 未结 5 1542
天涯浪人
天涯浪人 2021-02-09 05:59

I need to parse and encode to a legacy binary message format in Java. I began by using DataOutputStream to read/write primitive types but the problem I\'m having is that the mes

5条回答
  •  你的背包
    2021-02-09 06:31

    Just to add to pholser's answer, I think the Preon version would be something like this:

    class DataStructure {
      @BoundNumber(size="32")  long       first; // uint32
      @Bound                   boolean    second; // boolean
      @Bound                   boolean    third; // boolean
      @BoundNumber(size="32")  long       fourth; // uint32
      @BoundNumber(size="4")   int        fifth; // enum
      @BoundString(size="32")  String     sixth; // string
    }
    

    ... but in reality, you can make your life even easier by using Preon's support for dealing with enumerations directly.

    Creating a Codec for it and using it to decode some data would be something like this:

    Codec codec = Codecs.create(DataStructure.class)
    DataStructure data = Codecs.decode(codec, ....)
    

提交回复
热议问题