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.
The Framework is published in the Maven Central and can be easily added as a dependency into a maven project
45
42
```
46
43
<dependency>
@@ -51,8 +48,7 @@ The Framework is published in the Maven Central and can be easily added as a dep
51
48
```
52
49
the precompiled library jar, javadoc and sources also can be downloaded directly from [the Maven central.](http://search.maven.org/#browse|808871750)
53
50
54
-
Hello world
55
-
============
51
+
# Hello world
56
52
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.
57
53
58
54
The Easiest case below shows how to parse byte array to bits.
@@ -66,8 +62,7 @@ class Parsed {@Bin(type = BinType.BIT_ARRAY)byte[] parsed;}
# More compex example with features added as of 1.1.0
71
66
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
72
67
```Java
73
68
classFlags {
@@ -101,8 +96,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).
107
101
Field name must not be started by a number or chars '$' and '_'. *Field names are case insensitive!*
108
102
```
@@ -113,14 +107,14 @@ byte field3;
113
107
```
114
108

115
109
116
-
##Primitive types
110
+
##Primitive types
117
111
The Framework supports full set of Java numeric primitives with extra types like ubyte and bit.
118
112

119
-
##Complex types
113
+
##Complex types
120
114
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.
121
115

122
116
123
-
##Variable fields
117
+
##Variable fields
124
118
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.
125
119
```
126
120
final JBBPParser parser = JBBPParser.prepare("short k; var; int;");
@@ -140,15 +134,14 @@ If you have some data which structure is variable then you can use the `var` typ
140
134
```
141
135
*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.*
142
136
143
-
##Special types
137
+
##Special types
144
138
Special types makes some actions to skip data in input stream
145
139

146
-
##Byte order
140
+
##Byte order
147
141
Every multi-byte type can be read with different byte order.
148
142

149
143
150
-
Expressions
151
-
============
144
+
# Expressions
152
145
Expressions are used for calculation of length of arrays and allow brackets and integer operators which work similar to Java operators:
153
146
- Arithmetic operators: +,-,%,*,/,%
154
147
- Bit operators: &,|,^,~
@@ -164,25 +157,21 @@ int field1;
164
157
byte [field1+struct1.field2] data;
165
158
```
166
159
167
-
Commentaries
168
-
=============
160
+
# Commentaries
169
161
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.
170
162
```
171
163
int;
172
164
// hello commentaries
173
165
byte field;
174
166
```
175
167
176
-
Expression macroses
177
-
====================
168
+
# Expression macroses
178
169
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.
179
170
180
-
How to get result of parsing
181
-
=============================
171
+
# How to get result of parsing
182
172
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.
183
173
184
-
Example
185
-
========
174
+
# Example
186
175
The Example below shows how to parse a PNG file with the JBBP parser (the example taken from tests)
@@ -277,13 +266,14 @@ final JBBPParser tcpParser = JBBPParser.prepare(
277
266
278
267
finalJBBPFieldStruct result = pngParser.parse(tcpFrameStream);
279
268
```
269
+
# F.A.Q.
270
+
## Is it possible to use `@Bin` annotations for parsing and not only mapping?
271
+
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.
280
272
281
-
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
283
274
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.
284
275
285
-
I want to make a bin block instead of parsing!
286
-
===============================================
276
+
## I want to make a bin block instead of parsing!
287
277
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