kaitai-io / kaitai_struct_java_runtime

Kaitai Struct: runtime for Java
MIT License
42 stars 18 forks source link

Add expandable `KaitaiStream` backed by `List<Byte>` for serialization #41

Open pfroud opened 1 year ago

pfroud commented 1 year ago

To do serialization, it appears you need to know the total byte length in advance to allocate a ByteBufferKaitaiStream.

From http://doc.kaitai.io/serialization.html:

Current serialization support relies on fixed-length streams.... Therefore, you’ll often need to calculate sizes "manually" in your application.... The recommended way to do that is outlined in this GitHub comment.

This pull request is a proposal / starter implementation for a workaround: a KaitaiStream backed by a List<Byte> so it can expand.

It is for serialization only. For simplicity, all read methods are not implemented.

This is a temporary solution until Kaitai Struct can compute the length itself. Maybe someone will find it useful.

Example usage:

Example ks = new Example();
ks.setAnInteger(5);
ks._check();
try (WriteOnlyByteListKaitaiStream io = new WriteOnlyByteListKaitaiStream()) {
    ks._write(io);
    System.out.println(io.getList());
}

Or, if you want to provide your own List:

Example ks = new Example();
ks.setAnInteger(5);
ks._check();
List<Byte> byteList = new ArrayList<>();
try (KaitaiStream io = new WriteOnlyByteListKaitaiStream(byteList)) {
    ks._write(io);
}
System.out.println(byteList);
zhanghaocars commented 11 months ago

After some test, At least the bits in bytes function is not OK. I suggest to run more comprehensive tests.