I'm pleased to announce release 1.0.0 of Java Construct, a faithful port to Java of Python Construct. About Python Construct: http://construct.wikispaces.com/ "Construct is a python library for parsing and building of data structures (binary or textual). It is based on the concept of defining data structures in a declarative manner, rather than procedural code: more complex constructs are composed of a hierarchy of simpler ones. It's the first library that makes parsing fun, instead of the usual headache it is today." About Java Construct: https://github.com/ZiglioUK/construct/ This Java version employs some syntactic sugar (i.e. static methods) to make the syntax as close as possible to the original Construct library in Python. Example of a Construct:A Java Construct can parse byte arrays and produces Objects like Containers. Viceversa, it can take Objects to produce byte arrays.
import static construct.Core.*; import static construct.Macros.*; import static construct.Adapters.*; import static construct.lib.Containers.*; Construct struct = BitStruct( "foo", BitField("a", 3), Flag("b"), Padding(3), Nibble("c"), Struct("bar", Nibble("d"), Bit("e") ) );Parsing example:
public Object parse(byte data); public byte build( Object obj);Currently Java Construct supports enough Macros, Adapters and Repeaters to parse and build these protocols: Full ipstack example: https://github.com/ZiglioUK/construct/blob/master/src/main/construct/protocols/ipstack.java Notes: 1. I haven't tested for threadsafety but it's a priority 2. Streams are not supported so a message has to be contained in memory. If there are segments, they have to re-assambled prior to parsing 3. Text protocols like http are not supported, it's questionable whether Construct would be the right tool for text parsing.
Container c1 = Container( "a", 7, "b", false, "bar", Container( "d", 15 , "e", 1 ), "c",8 ); Container c2 = struct.parse( ByteArray( 0xe1, 0x1f )); assertEquals( c1, c2 );