0

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.

bencollier
  • 53
  • 1
  • 6
  • 1
    Possible duplicate of [Does Java support structs?](https://stackoverflow.com/questions/5168144/does-java-support-structs) – Benjamin Urquhart Apr 06 '19 at 14:49
  • It doesn't have to be a Struct, but I'd like to label the bits and bytes within a bit level data structure. – bencollier Apr 06 '19 at 14:56
  • The closest you will get is serialization or a custom implementation – Benjamin Urquhart Apr 06 '19 at 18:10
  • There are lots of existing tools to design data structures that can be serialized efficiently, e.g. https://developers.google.com/protocol-buffers/. There are not a lot of tools that let you control the exact serialization structure. – Louis Wasserman Apr 06 '19 at 21:28

1 Answers1

0

I do not know the answer to your actual question.

I will offer, however, a thought or two about the nature of the question itself. C was not developed as a high-level language -- the best description I've heard of it is a "structured assembler", it has operators that were based on addressing modes available on the 16-bit machines on which it was first developed, and was not developed as a standard to be used in applications but as something (much) easier than assembler that still allowed the programmer enough control to write very efficient code. The first two things done with it were a compiler and an operating system, so runtime efficiency was vital (in the early 70s) in ways that no one but mobile and embedded developers can begin to appreciate these days.

The "order the bytes are defined in the structure" is not, to my mind, a good way to think of the data in Java -- the programmer does not know or care the order in which the fields in his objects are stored, or whether they're stored at all -- it isn't part of the language definition. It seems to me any library or whatever that claimed to do this would have to put in disclaimers and/or have its own compiler; while I don't know of any reason it could not do so and follow the Java specification, I don't know why someone would bother.

So ask yourself why you want to do this. And if you have a good answer, put it in here; I'd be curious.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • I'm building data structures to be streamed over raw TCP. – bencollier Apr 06 '19 at 15:01
  • Interesting. I think the answer to your question, then, is "no". You need something outside of the Java language to make the data structure definition the way you want it. – arcy Apr 06 '19 at 15:20