kaitai_struct_java_runtime icon indicating copy to clipboard operation
kaitai_struct_java_runtime copied to clipboard

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

Open pfroud opened this issue 2 years ago • 1 comments

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);

pfroud avatar Aug 22 '23 00:08 pfroud

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

zhanghaocars avatar Oct 30 '23 09:10 zhanghaocars