You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Java has some embedded features to parse binary data (for instance ByteBuffer), but I wanted to work with separated bits and describe binary structure in some strong DSL(domain specific language). I was very impressed by the [the Python Struct package](https://docs.python.org/2/library/struct.html) package so that I decided to make something like that. So JBBP was born.<br>
14
13
p.s.<br>
15
14
For instance I have been very actively using the framework in [the ZX-Poly emulator](https://github.com/raydac/zxpoly) to parse snapshot files and save results.
@@ -44,8 +43,7 @@ Change log
44
43
-**1.0 (08-AUG-2014)**
45
44
- The Initial version
46
45
47
-
Maven dependency
48
-
======================
46
+
# Maven dependency
49
47
The Framework is published in the Maven Central and can be easily added as a dependency into a maven project
50
48
```
51
49
<dependency>
@@ -56,8 +54,7 @@ The Framework is published in the Maven Central and can be easily added as a dep
56
54
```
57
55
the precompiled library jar, javadoc and sources also can be downloaded directly from [the Maven central.](http://search.maven.org/#browse|808871750)
58
56
59
-
Hello world
60
-
============
57
+
# Hello world
61
58
The Framework is very easy in use because it has only two main classes for its functionality com.igormaznitsa.jbbp.JBBPParser (for data parsing) and com.igormaznitsa.jbbp.io.JBBPOut (for binary block writing), both of them work over low-level IO classes com.igormaznitsa.jbbp.io.JBBPBitInputStream and com.igormaznitsa.jbbp.io.JBBPBitOutputStream which are the core for the framework.
62
59
63
60
The Easiest case below shows how to parse byte array to bits.
@@ -71,8 +68,7 @@ class Parsed {@Bin(type = BinType.BIT_ARRAY)byte[] parsed;}
# More compex example with features added as of 1.1.0
76
72
The Example shows how to parse a byte written in non-standard MSB0 order (Java has LSB0 bit order) to bit fields, print its values and pack fields back
77
73
```Java
78
74
classFlags {
@@ -106,8 +102,7 @@ The Example will print in console the text below
Every field can have case insensitive name which should not contain '.' (because it is reserved for links to structure field values) and '#'(because it is also reserver for inside usage).
112
107
Field name must not be started by a number or chars '$' and '_'. *Field names are case insensitive!*
113
108
```
@@ -118,14 +113,14 @@ byte field3;
118
113
```
119
114

120
115
121
-
##Primitive types
116
+
##Primitive types
122
117
The Framework supports full set of Java numeric primitives with extra types like ubyte and bit.
123
118

124
-
##Complex types
119
+
##Complex types
125
120
The Framework provides support for arrays and structures. Just keep in mind that in expressions you can make links to field values only defined before expression.
126
121

127
122
128
-
##Variable fields
123
+
##Variable fields
129
124
If you have some data which structure is variable then you can use the `var` type for defined field and process reading of the data manually with custom [JBBPVarFieldProcessor](https://github.com/raydac/java-binary-block-parser/blob/master/src/main/java/com/igormaznitsa/jbbp/JBBPVarFieldProcessor.java) instance.
130
125
```
131
126
final JBBPParser parser = JBBPParser.prepare("short k; var; int;");
@@ -145,15 +140,14 @@ If you have some data which structure is variable then you can use the `var` typ
145
140
```
146
141
*NB! Some programmers trying to use only parser for complex data, it is mistake. In the case it is much better to have several easy parsers working with the same [JBBPBitInputStream](https://github.com/raydac/java-binary-block-parser/blob/master/src/main/java/com/igormaznitsa/jbbp/io/JBBPBitInputStream.java) instance, it allows to keep decision points on Java level and make solution easier.*
147
142
148
-
##Special types
143
+
##Special types
149
144
Special types makes some actions to skip data in input stream
150
145

151
-
##Byte order
146
+
##Byte order
152
147
Every multi-byte type can be read with different byte order.
153
148

154
149
155
-
Expressions
156
-
============
150
+
# Expressions
157
151
Expressions are used for calculation of length of arrays and allow brackets and integer operators which work similar to Java operators:
158
152
- Arithmetic operators: +,-,%,*,/,%
159
153
- Bit operators: &,|,^,~
@@ -169,25 +163,21 @@ int field1;
169
163
byte [field1+struct1.field2] data;
170
164
```
171
165
172
-
Commentaries
173
-
=============
166
+
# Commentaries
174
167
You can use commentaries inside a parser script, the parser supports the only comment format and recognizes as commentaries all text after '//' till the end of line.
175
168
```
176
169
int;
177
170
// hello commentaries
178
171
byte field;
179
172
```
180
173
181
-
Expression macroses
182
-
====================
174
+
# Expression macroses
183
175
Inside expression you can use field names and field paths, also you can use the special macros '$$' which represents the current input stream byte counter, all fields started with '$' will be recognized by the parser as special user defined variables and it will be requesting them from special user defined provider. If the array size contains the only '_' symbol then the field or structure will not have defined size and whole stream will be read.
184
176
185
-
How to get result of parsing
186
-
=============================
177
+
# How to get result of parsing
187
178
The Result of parsing is an instance of com.igormaznitsa.jbbp.model.JBBPFieldStruct class which represents the root invisible structure for the parsed data and you can use its inside methods to find desired fields for their names, paths or classes. All Fields are successors of com.igormaznitsa.jbbp.model.JBBPAbstractField class. To increase comfort, it is easier to use mapping to classes when the mapper automaticaly places values to fields of a Java class.
188
179
189
-
Example
190
-
========
180
+
# Example
191
181
The Example below shows how to parse a PNG file with the JBBP parser (the example taken from tests)
@@ -282,13 +272,14 @@ final JBBPParser tcpParser = JBBPParser.prepare(
282
272
283
273
finalJBBPFieldStruct result = pngParser.parse(tcpFrameStream);
284
274
```
275
+
# F.A.Q.
276
+
## Is it possible to use `@Bin` annotations for parsing and not only mapping?
277
+
No, `@Bin` annotations in classes are used only for mapping and data writing, but there is [the code snippet](https://gist.github.com/raydac/28d770307bd33683aa17ea3c39d5e2c4) allows to generate JBBP DSL based on detected @Bin annotations in class.
285
278
286
-
My Binary data format is too complex one to be decoded by a JBBP script
## My Binary data format is too complex one to be decoded by a JBBP script
288
280
No problems! The Parser works over com.igormaznitsa.jbbp.io.BitInputStream class which can be used directly and allows read bits, bytes, count bytes and align data from a stream.
289
281
290
-
I want to make a bin block instead of parsing!
291
-
===============================================
282
+
## I want to make a bin block instead of parsing!
292
283
The Framework contains a special helper as the class com.igormaznitsa.jbbp.io.JBBPOut which allows to build bin blocks with some kind of DSL
0 commit comments