Java equivalent of Python's “construct” library

强颜欢笑 提交于 2019-12-05 22:01:24

I've looked a lot around and all I could find was Ragel (www.complang.org/ragel), that can also produce Java code. It looked too complex for me so I've started some work to port Construct to Java. I suspect it would be easier to make something like that in Scala, Groovy or JavaScript.

Construct on GitHub: https://github.com/MostAwesomeDude/construct

java construct: https://github.com/ZiglioNZ/construct

I've spent a couple of days on it, mostly looking for equivalents of python's expressive classes. The most useful java classes I've found are: java.util.Scanner, java.util.Formatter and java.nio.ByteBuffer. It's a big task so I want to focus on something small like creating simple parsers and formatters for ByteBuffers.

[Update]

I've ported enough code to parse and build some of the protocols that come with Python Construct, such as ethernet, arp and ipv4. Check it out at https://github.com/ZiglioNZ/construct

[Update: new Release]

Java Construct 1.1.2 is now available, see release notes.

You can use DataInput/DataOutput (and their implementations) to convert any set of values from/to a set of bytes. This doesn't give you an object where you can use names to access the individual fields, though - you would have to create such yourself.

It depends a bit on what you want to do - do you have a fixed data format to send/receive on wire, or does this vary from time to time?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!