-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang_java.html
422 lines (422 loc) · 19.4 KB
/
lang_java.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.23">
<title>Kaitai Struct: Java notes</title>
<link rel="stylesheet" href="styles/pygments-default.css">
<link rel="stylesheet" href="styles/bootstrap.min.css">
<link rel="stylesheet" href="styles/bootstrap-theme.min.css">
<link rel="stylesheet" href="styles/main.css">
<link rel="stylesheet" href="styles/pygments-default.css">
<link rel="stylesheet" href="./styles/colony.css">
<link rel="stylesheet" href="styles/asciidoctor-tabs.css">
</head>
<body class="article toc2 toc-left">
<nav class="navbar navbar-inverse navbar-fixed-top" id="main-navbar">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#main-navbar-collapse" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">Kaitai Struct</span>
</div>
<div class="collapse navbar-collapse" id="main-navbar-collapse">
<ul class="nav navbar-nav">
<li class=""><a href="//kaitai.io/#what-is-it">What is it?</a></li>
<li class=""><a href="//kaitai.io/#quick-start">Quick Start</a></li>
<li class=""><a href="//kaitai.io/#download">Download</a></li>
<li class=""><a href="//kaitai.io/news/">News</a></li>
<li class=""><a href="//formats.kaitai.io/">Format Gallery</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a href="https://ide.kaitai.io/">Try it — Web IDE</a></li>
<li class="active"><a href="index.html">Documentation</a></li>
</ul>
</div>
</div>
</nav>
<div id="header">
<h1>Kaitai Struct: Java notes</h1>
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel1">
<li><a href="#_source_files_generation">Source files generation</a></li>
<li><a href="#_usage_patterns">Usage patterns</a></li>
<li><a href="#_runtime_library">Runtime library</a>
<ul class="sectlevel2">
<li><a href="#_installation">Installation</a></li>
<li><a href="#_api">API</a></li>
</ul>
</li>
<li><a href="#_naming">Naming</a></li>
<li><a href="#_types">Types</a></li>
<li><a href="#_attributes">Attributes</a></li>
<li><a href="#_instances">Instances</a></li>
<li><a href="#_enums">Enums</a></li>
<li><a href="#_primitive_type_mapping">Primitive type mapping</a>
<ul class="sectlevel2">
<li><a href="#_string_encoding">String encoding</a></li>
</ul>
</li>
<li><a href="#_array_types">Array types</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect1">
<h2 id="_source_files_generation">Source files generation</h2>
<div class="sectionbody">
<div class="paragraph">
<p>To use Kaitai Struct specifications in Java, one must compile .ksy
specs to .java classes implementing parsing/serialization logic first:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>This can be done manually: by <a href="user_guide.html#invocation">invoking
kaitai-struct-compiler directly</a>, and adding generated files to
your project.</p>
</li>
<li>
<p>For certain build pipelines, the process can be automated by
introducing this generation step into build flow:</p>
<div class="ulist">
<ul>
<li>
<p>for <a href="https://maven.apache.org">Apache Maven</a>: <a href="https://github.com/valery1707/kaitai-maven-plugin">kaitai-maven-plugin</a></p>
</li>
<li>
<p>for <a href="https://gradle.org">Gradle</a>: <a href="https://github.com/valery1707/kaitai-gradle-plugin">kaitai-gradle-plugin</a></p>
</li>
</ul>
</div>
</li>
</ul>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_usage_patterns">Usage patterns</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Parsing a structure directly from a local file:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><span></span><span class="tok-n">AnExampleClass</span><span class="tok-p">.</span><span class="tok-na">fromFile</span><span class="tok-p">(</span><span class="tok-s">"an_example.data"</span><span class="tok-p">)</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Parsing a structure from a byte array (<code>byte[]</code>):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><span></span><span class="tok-k">new</span><span class="tok-w"> </span><span class="tok-n">AnExampleClass</span><span class="tok-p">(</span><span class="tok-k">new</span><span class="tok-w"> </span><span class="tok-n">KaitaiStream</span><span class="tok-p">(</span><span class="tok-n">byteArray</span><span class="tok-p">))</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Note that parsing from non-seekable streams (i.e.
<a href="https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html">FileInputStream</a>,
<a href="https://docs.oracle.com/javase/7/docs/api/java/io/BufferedInputStream.html">BufferedInputStream</a>,
etc) is not supported and probably won’t be supported, as a lot of
parsing functionality in KS relies on seek support.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_runtime_library">Runtime library</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_installation">Installation</h3>
<div class="paragraph">
<p>Generated code for Java relies on
<a href="https://github.com/kaitai-io/kaitai_struct_java_runtime">Kaitai Struct
runtime library for Java</a>. It is a small, MIT-licensed library, which
is
<a href="https://search.maven.org/#search%7Cga%7C1%7Ca%3A%22kaitai-struct-runtime%22">published
in Maven’s Central Repository</a>, so typically it’s enough to add the
following to one’s <code>pom.xml</code>:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="xml"><span></span><span class="tok-nt"><dependency></span>
<span class="tok-w"> </span><span class="tok-nt"><groupId></span>io.kaitai<span class="tok-nt"></groupId></span>
<span class="tok-w"> </span><span class="tok-nt"><artifactId></span>kaitai-struct-runtime<span class="tok-nt"></artifactId></span>
<span class="tok-w"> </span><span class="tok-nt"><version></span>0.10<span class="tok-nt"></version></span>
<span class="tok-nt"></dependency></span></code></pre>
</div>
</div>
<div class="paragraph">
<p>For other build tools, such as Ivy, SBT, Gradle, Leiningen, Buildr,
etc, please consult Central Repository’s page for exact instructions.</p>
</div>
<div class="paragraph">
<p>Alternatively, one can just copy whole source code to one’s project:
the library is intentionally kept as only 2 .java files, so it should
be easy enough as well.</p>
</div>
</div>
<div class="sect2">
<h3 id="_api">API</h3>
<div class="paragraph">
<p>Following most other runtimes example, everything revolves around two
basic classes:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>KaitaiStruct — a common superclass for all classes that represent
user types in KS. Java implementation is very limited and basically
only keeps <code>_io</code> member of type KaitaiStream and provides a getter
for it.</p>
</li>
<li>
<p>KaitaiStream — a useful abstraction of seekable input stream that
can be read with <a href="stream_api.html">Kaitai Struct stream API</a>
(i.e. methods like <code>readU4le()</code>. Internally, it uses a
<a href="https://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html">ByteBuffer</a>
(either a
<a href="https://docs.oracle.com/javase/7/docs/api/java/nio/MappedByteBuffer.html">MappedByteBuffer</a>
backed by
<a href="https://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html">FileChanel</a>
for parsing local files, or a regular wrapper over a given byte
array), so it can work on both local files and in-memory data.</p>
</li>
</ul>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_naming">Naming</h2>
<div class="sectionbody">
<div class="paragraph">
<p>KS tries to follow mandatory and recommended Java practices as close as
possible.</p>
</div>
<div class="paragraph">
<p>Class names would be represented in upper camel case (i.e.
<code>an_example_class</code> ⇒ <code>AnExampleClass</code>).</p>
</div>
<div class="paragraph">
<p>All attributes and instance names use lower camel case (i.e.
<code>an_example_attribute</code> ⇒ <code>anExampleAttribute</code>).</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_types">Types</h2>
<div class="sectionbody">
<div class="paragraph">
<p>All user types are mapped 1-to-1 to relevant Java classes. Nested types
are mapped to nested classes, i.e. for nested types like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="yaml"><span></span><span class="tok-nt">meta</span><span class="tok-p">:</span>
<span class="tok-w"> </span><span class="tok-nt">id</span><span class="tok-p">:</span><span class="tok-w"> </span><span class="tok-l tok-l-Scalar tok-l-Scalar-Plain">parent</span>
<span class="tok-c1"># ...</span>
<span class="tok-nt">types</span><span class="tok-p">:</span>
<span class="tok-w"> </span><span class="tok-nt">child</span><span class="tok-p">:</span>
<span class="tok-w"> </span><span class="tok-c1"># ...</span>
<span class="tok-w"> </span><span class="tok-nt">types</span><span class="tok-p">:</span>
<span class="tok-w"> </span><span class="tok-nt">grandchild</span><span class="tok-p">:</span>
<span class="tok-w"> </span><span class="tok-c1"># ...</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>one can expect to get the following class structure:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><span></span><span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-kd">class</span> <span class="tok-nc">Parent</span><span class="tok-w"> </span><span class="tok-kd">extends</span><span class="tok-w"> </span><span class="tok-n">KaitaiStruct</span><span class="tok-w"> </span><span class="tok-p">{</span>
<span class="tok-w"> </span><span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-kd">static</span><span class="tok-w"> </span><span class="tok-kd">class</span> <span class="tok-nc">Child</span><span class="tok-w"> </span><span class="tok-kd">extends</span><span class="tok-w"> </span><span class="tok-n">KaitaiStruct</span><span class="tok-w"> </span><span class="tok-p">{</span>
<span class="tok-w"> </span><span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-kd">static</span><span class="tok-w"> </span><span class="tok-kd">class</span> <span class="tok-nc">GrandChild</span><span class="tok-w"> </span><span class="tok-kd">extends</span><span class="tok-w"> </span><span class="tok-n">KaitaiStruct</span><span class="tok-w"> </span><span class="tok-p">{</span>
<span class="tok-w"> </span><span class="tok-p">}</span>
<span class="tok-w"> </span><span class="tok-p">}</span>
<span class="tok-p">}</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Every generated class will have 3 constructors and a static factory
method (plus a private <code>_read()</code> method that is invoked from all the
constructors to do actual parsing):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="pygments highlight"><code data-lang="java"><span></span><span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-nf">AnExampleClass</span><span class="tok-p">(</span><span class="tok-n">KaitaiStream</span><span class="tok-w"> </span><span class="tok-n">_io</span><span class="tok-p">)</span>
<span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-nf">AnExampleClass</span><span class="tok-p">(</span><span class="tok-n">KaitaiStream</span><span class="tok-w"> </span><span class="tok-n">_io</span><span class="tok-p">,</span><span class="tok-w"> </span><span class="tok-n">KaitaiStruct</span><span class="tok-w"> </span><span class="tok-n">_parent</span><span class="tok-p">)</span>
<span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-nf">AnExampleClass</span><span class="tok-p">(</span><span class="tok-n">KaitaiStream</span><span class="tok-w"> </span><span class="tok-n">_io</span><span class="tok-p">,</span><span class="tok-w"> </span><span class="tok-n">KaitaiStruct</span><span class="tok-w"> </span><span class="tok-n">_parent</span><span class="tok-p">,</span><span class="tok-w"> </span><span class="tok-n">AnExampleClass</span><span class="tok-w"> </span><span class="tok-n">_root</span><span class="tok-p">)</span>
<span class="tok-kd">public</span><span class="tok-w"> </span><span class="tok-kd">static</span><span class="tok-w"> </span><span class="tok-n">AnExampleClass</span><span class="tok-w"> </span><span class="tok-nf">fromFile</span><span class="tok-p">(</span><span class="tok-n">String</span><span class="tok-w"> </span><span class="tok-n">fileName</span><span class="tok-p">)</span></code></pre>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_attributes">Attributes</h2>
<div class="sectionbody">
<div class="paragraph">
<p>Sequence attribute parsing is done in <code>_read()</code> method which is
typically invoked from a constructor. All parsed attributes are stored
as private member variables.</p>
</div>
<div class="paragraph">
<p>For all attributes, a relevant getter method will be generated, so an
attribute can be accessed outside of class like
<code>classInstance.anExampleAttribute()</code>.</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_instances">Instances</h2>
<div class="sectionbody">
<div class="paragraph">
<p>TODO</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_enums">Enums</h2>
<div class="sectionbody">
<div class="paragraph">
<p>TODO</p>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_primitive_type_mapping">Primitive type mapping</h2>
<div class="sectionbody">
<div class="paragraph">
<p>There are several things of note that influence mapping KS types to Java
types:</p>
</div>
<div class="ulist">
<ul>
<li>
<p>There are no support for unsigned integer types in Java. In some cases
it’s no big deal, but some use cases (for example, comparison or bit
shifts) may be severely hindered by that issue. KS tries to make up for
that fact by using larger signed types where that’s possible and
reasonable to do. Where it’s not possible (i.e. 64-bit unsigned integers
— <code>u8</code>), KS would use signed <code>long</code> type.</p>
</li>
<li>
<p>Java has 2 types for every numeric type: "primitive" type (i.e. <code>int</code>)
and "reference" type (i.e. <code>Integer</code>) — the latter being a full-featured
object that can have <code>null</code> assigned to it and stored in collections.
It’s not practical to use reference types everywhere, so KS makes use of
them only in the following situations:</p>
</li>
<li>
<p>when data type is used as part of a collection</p>
</li>
<li>
<p>when it’s possible that a particular attribute / instance will be
unassigned (i.e. because of [[if|attribute description#if]] expression)
— <code>null</code> will be returned in this case</p>
</li>
</ul>
</div>
<div class="paragraph">
<p>The overall primitive type mapping goes as follows:</p>
</div>
<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 33.3333%;">
<col style="width: 33.3334%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top"><code>type</code></th>
<th class="tableblock halign-left valign-top">Java primitive type</th>
<th class="tableblock halign-left valign-top">Java reference type</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">no type</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">byte[]</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">byte[]</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>u1</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">int</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Integer</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>u2</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">int</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Integer</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>u4</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">long</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Long</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>u8</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">long</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Long</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>s1</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">byte</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Byte</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>s2</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">short</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Short</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>s4</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">int</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Integer</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>s8</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">long</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">Long</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>str</code>, <code>strz</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">String</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">String</p></td>
</tr>
</tbody>
</table>
<div class="sect2">
<h3 id="_string_encoding">String encoding</h3>
<div class="paragraph">
<p>Encoding a stream of bytes into a <code>String</code> is done with the standard
Java API:
<a href="https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#String(byte%5B%5D,%20java.nio.charset.Charset)">String
method constructor</a></p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_array_types">Array types</h2>
<div class="sectionbody">
<div class="paragraph">
<p>All repetitions in Java are translated to <code>ArrayList<~></code></p>
</div>
</div>
</div>
</div>
<div id="footer">
© 2015–2024 Kaitai Project
</div>
<script src="js/jquery-1.12.3.min.js"></script>
<script src="js/bootstrap.min.js"></script>
<script src="js/asciidoctor-tabs.js"></script>
</body>
</html>