Comments (6)
the DSL is very easy and linear one so that all decision points must be implemented on the Java level, sometime if you have a field with variable data content you can use var
modifier and use custom var field processor but I would recommend to have several JBBPParsers for different cases working with the same JBBPBitInputStream and it allows to build more or less complex not over-engineered solutions.
what do you mean as inheritance of mapped class? it would be good just provide some example of code
from java-binary-block-parser.
I also considered the approach of different parsers on one stream (like in the Ethernet/IP/TCP example), but that seems to be quite a mix of descriptional modelling and imperative programming, which I personally do not like very much.
So, to give some example of what I mean with keeping the single classes separate, but together with their binary description:
https://github.com/synnefy/test_jbbp/blob/master/src/main/java/io/synnefy/test/jbbp/Png.java
Here I just build the parser description by concatenation of the part defined in the Chunk class, and later create a parser from it. It works, but I need to manually reference the right description, which is error prone.
I personally would prefer to be a able to declare Chunk itself as a "binary" type in the DSL, which could be reused and resolved automatically like any other primitive field, e.g.:
https://github.com/synnefy/test_jbbp/blob/master/src/main/java/io/synnefy/test/jbbp/PngDefunct.java
This would then also allow automatic inheritance, in which only additional fields of a class need to be declared (if there are any).
Are there any plans to support such a feature?
I had a look at preon, which describes the binary mapping by annotations on each class member (much like JPA for databases), allows to nest and inherit classes, and builds the binary structure accordingly. Unfortunately, it currently does not seem to be maintained, nor is there good support for unsigned binary representations, so a combination of both frameworks would be great :).
from java-binary-block-parser.
if take in count that parser works with text script, from my point of view it is much easier to use the edge instead of implementation work with java classes, you can make script dynamically and for instance I rewrote PNG parser where format of chunk described in the chunk class
public class PngTest {
@Bin
private static class Chunk {
public static final String FORMAT = "int length; int type; byte[length] data; int crc;";
int length;
int type;
byte[] data;
int crc;
}
@Bin
private static class PNGFormat {
public static final String FORMAT = "long header; chunks [_]{$"+Chunk.class.getSimpleName()+"$}";
long header;
Chunk [] chunks;
private static final JBBPParser PARSER = JBBPParser.prepare(FORMAT.replace("$"+Chunk.class.getSimpleName()+"$", Chunk.FORMAT));
public PNGFormat(final InputStream inStream) throws IOException {
PARSER.parse(inStream).mapTo(this);
}
}
public static void main(String ... args) throws Exception {
final PNGFormat parser = new PNGFormat(ClassLoader.getSystemClassLoader().getResourceAsStream("test.png"));
System.out.println("Header : "+parser.header);
System.out.println("Number of chunks : "+parser.chunks.length);
}
}
it is very hard to implement type system related to java classes and it will increase complexity of the library dramatically without big profit
from java-binary-block-parser.
Thank you for the example, using the class lookup might help and reduce possibility for errors.
There is another detail which I currently do not understand well: From the examples, it seesm to me that the binary format for the output is described by the parameters for the Bin()-Annotation (like BinType, outOrder...), whereas the binary format for the input is described by the argument to Parser.prepare(). Is it possible to use the annotions for parsing, or the DSL for encoding, so to have only one source for the binary format description?
from java-binary-block-parser.
at first there was not @bin annotation in the library and the library could only parse data, then some functionality to write data and mapping was added and @bin annotation was added for the purpose. @bin provides many parameters but anyway it doesn't allow correctly build script, for instance it is impossible to describe how to code array length for fields but if you have more or less easy case then you can try to use the snippet to generate DSL from fields marked by @bin , the snippet will generate DSL from such classes for such case
public static class Chunk {
public static final String FORMAT = "int length; int type; byte[length] data; int crc;";
@Bin(outOrder = 1)
int length;
@Bin(outOrder = 2)
int type;
@Bin(outOrder = 3, type = BinType.BYTE_ARRAY,comment = "length")
byte[] data;
@Bin(outOrder = 4)
int crc;
}
@Bin
public static class PNGFormat {
public static final String FORMAT = "long header; chunks [_]{$" + Chunk.class.getSimpleName() + "$}";
@Bin(outOrder = 1)
long header;
@Bin(outOrder = 2, type = BinType.STRUCT_ARRAY)
Chunk[] chunks;
private static final JBBPParser PARSER = JBBPParser.prepare(FORMAT.replace("$" + Chunk.class.getSimpleName() + "$", Chunk.FORMAT));
public PNGFormat(final InputStream inStream) throws IOException {
PARSER.parse(inStream).mapTo(this);
}
}
from java-binary-block-parser.
Thank you, this is very helpful for me. I guess it might also be useful to add it as an example to the library itself, as other people might want also want to use it for parsing and serialization.
from java-binary-block-parser.
Related Issues (20)
- NullPointerException when using new JBBPDslBuilder HOT 5
- "Expression is empty" error when using arrays HOT 8
- PatternSyntaxException when use on Android. HOT 2
- support multiple custom type processors? HOT 1
- Illegal reflective access reported when used with JDK 11 HOT 2
- Bug in parsing of stringj written in MSB0 HOT 5
- Strings in other codecs HOT 3
- JBBPByteOrder may add nativeOrder and inheritOrder HOT 3
- Processing Nullable fields exceptions HOT 8
- Issue with simple object parse HOT 2
- how to set JBBPByteOrder for JBBPParser ? HOT 3
- How to read/parse stringj when here is no '3' mark? HOT 3
- parse dns query data packge
- Filter fields for JBBPOut HOT 3
- Could you please add unsigned int32 support? HOT 4
- Error running gradle plugin task jbbpGenerate in gradle 7.4.2 HOT 3
- Really great project!
- Gradle plugin error: Cannot add extension with name 'jbbp', as there is an extension already registered with that name HOT 5
- Use child class for parsing will throw java.lang.IllegalStateException: There is not any opened struct HOT 4
- Unpack float with endian HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from java-binary-block-parser.