Byte array with an `_io` member: Java parsing library

Helper type to work around Kaitai Struct not providing an _io member for plain byte arrays.

KS implementation details

License: MIT

This page hosts a formal specification of Byte array with an `_io` member using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for Java generated by Kaitai Struct depends on the Java runtime library. You have to install it before you can parse data.

The Java runtime library is published in the Maven Central Repository. Refer to the artifact page for instructions how to add it into your project with the build tool that you use.

Code

Parse a local file and get structure in memory:

BytesWithIo data = BytesWithIo.fromFile("path/to/local/file.Byte array with an `_io` member");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
BytesWithIo data = new BytesWithIo(new ByteBufferKaitaiStream(someArray));

After that, one can get various attributes from the structure by invoking getter methods like:

data.data() // => The actual data.

Java source code to parse Byte array with an `_io` member

BytesWithIo.java

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

import io.kaitai.struct.ByteBufferKaitaiStream;
import io.kaitai.struct.KaitaiStruct;
import io.kaitai.struct.KaitaiStream;
import java.io.IOException;


/**
 * Helper type to work around Kaitai Struct not providing an `_io` member for plain byte arrays.
 */
public class BytesWithIo extends KaitaiStruct {
    public static BytesWithIo fromFile(String fileName) throws IOException {
        return new BytesWithIo(new ByteBufferKaitaiStream(fileName));
    }

    public BytesWithIo(KaitaiStream _io) {
        this(_io, null, null);
    }

    public BytesWithIo(KaitaiStream _io, KaitaiStruct _parent) {
        this(_io, _parent, null);
    }

    public BytesWithIo(KaitaiStream _io, KaitaiStruct _parent, BytesWithIo _root) {
        super(_io);
        this._parent = _parent;
        this._root = _root == null ? this : _root;
        _read();
    }
    private void _read() {
        this.data = this._io.readBytesFull();
    }
    private byte[] data;
    private BytesWithIo _root;
    private KaitaiStruct _parent;

    /**
     * The actual data.
     */
    public byte[] data() { return data; }
    public BytesWithIo _root() { return _root; }
    public KaitaiStruct _parent() { return _parent; }
}