Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: serialization
Choose a base branch
from

Conversation

pfroud
Copy link

@pfroud pfroud commented Aug 22, 2023

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 pfroud changed the base branch from master to serialization August 22, 2023 00:54
@zhanghaocars
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants