首页 > 解决方案 > Byte (or bit) sequences in Java and Kotlin

问题描述

Is there a canonical way to define a data structure in Java (and by extension Kotlin) which can be serialized into a byte array or sequence of bits in the order the bytes are defined in the structure?

Similar to a Struct in C.

Edit: So, further to this, I would like a simple and expressive way of defining a data structure and then accessing any part of it. So, for instance, in pseudocode:

DataStructure Message {
    Bit newFlag;
    Bit genderFlag;
    Bit sizeFlag;
    Bit activeFlag;
    Bit[4] operation;
    Byte messageSize;
    Byte[] message; 
}

So then we do:

Message firstMessage = new Message(1, 0, 1, 0, b'0010', 11, "Hello there");

And we can say:

ByteArray serialisedMessage = firstMessage.toBytes();

Which would give us an array which looked like:

[b'10100010', b'00001011', "Hello there" (but in bytes)]

Then we could do:

firstMessage.genderFlag = 1;

..and just rerun .toBytes on the object.

Obviously there are a million ways of doing stuff like this in Java, but nothing syntactically simple, as far as I can see - pretty much anything would involve having to write a custom serialisation (and not object serialisation as per Java) method for each object. Perhaps that's the canonical way to do this, but it would be nice to have it simpler, as per C, Rust, and, erm, COBOL.

标签: javaarrayskotlinbit-manipulationbyte

解决方案


我不知道你的实际问题的答案。

但是,我将就问题本身的性质提出一两个想法。C 不是作为高级语言开发的——我听说过的最好的描述是“结构化汇编程序”,它具有基于最初开发它的 16 位机器上可用的寻址模式的运算符,并且不是作为应用程序中使用的标准开发的,而是作为比汇编程序更容易的东西,它仍然允许程序员有足够的控制权来编写非常有效的代码。用它完成的前两件事是编译器和操作系统,因此运行时效率至关重要(在 70 年代初期),如今只有移动和嵌入式开发人员才能开始意识到这一点。

在我看来,“在结构中定义字节的顺序”并不是思考 Java 中数据的好方法——程序员不知道也不关心对象中字段的存储顺序,或者无论它们是否被存储——它不是语言定义的一部分。在我看来,任何图书馆或声称这样做的任何东西都必须加入免责声明和/或拥有自己的编译器;虽然我不知道它不能这样做并遵循 Java 规范的任何原因,但我不知道为什么有人会打扰。

所以问问自己为什么要这样做。如果你有一个好的答案,把它放在这里;我会很好奇。


推荐阅读