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);
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:
This pull request is a proposal / starter implementation for a workaround: a
KaitaiStream
backed by aList<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:
Or, if you want to provide your own
List
: