-1

I recently started kaitai-struct for dealing with arbitrary binary formats. I have created the .ksy file for my data and parsed it to targeted language that is java. Now can anyone point me how to pass the input file that has the data and how to get the data that is parsed as output so that I can write code to manipulate that data to my requirements? Is there any tutorial on how to write code depending on the data we get.

Thanks in advance.

Srikar
  • 351
  • 5
  • 16

2 Answers2

2

First you have to generate Java classes from the .ksy file using the Kaitai Struct Compiler or the WebIDE. You can find more information how to use the compiler in the Kaitai user guide.

If you use the WebIDE then just simply right-click on your .ksy file and select the Generate parser > Java menu item.

After you have the generated Java code, you can parse a structure directly from a local file like this:

AnExampleClass output = AnExampleClass.fromFile("an_example.data");
// ... manipulate output ...

Or you can parse a structure from a byte array (byte[]):

AnExampleClass output = new AnExampleClass(new KaitaiStream(byteArray));
// ... manipulate output ...

Note that parsing from non-seekable streams (i.e. FileInputStream, BufferedInputStream, etc) is not supported and probably won’t be supported, as a lot of parsing functionality in KS relies on seek support.

You can read the generic documentation how to use the API here and you can find the Java-specific documentation here.

koczkatamas
  • 156
  • 1
  • 3
  • 1
    Can you please give me an example like a snippet of code. I've read the documentation but couldn't get exactly what it's saying. A small snippet can help me a lot, thanks in advance – Srikar Dec 14 '17 at 10:45
  • I don't understand where you got stuck. For example have you tried to use the code snippets I just posted in the answer you replied to? And if you tried then where you got stuck? What's the error message, etc? – koczkatamas Dec 14 '17 at 12:50
  • yeah its working thanks, i just got confused at some point so I couldn't understand what I was doing. But after reading the documentation for sometime i got the git of it. – Srikar Dec 15 '17 at 17:25
0

The answer from koczkatamas is outdated. There are now specific implementations.

The snippet would be

AnExampleClass output = new AnExampleClass(new ByteBufferKaitaiStream(byteArray));

See this issue for more details

Lele
  • 1
  • 2