[英]Java equivalent of Python's “construct” library
Is there a Java equivalent of Python's "construct" library? 是否有Java的“构造”库的Java等价物? I want to write "structs" like so:
我想像这样写“结构”:
message = Struct("message",
UBInt8("protocol"),
UBInt16("length"),
MetaField("data", lambda ctx: ctx["length"])
)
It doesn't have to specifically be a library with some sort of abstraction using the Java language. 它不必特别是使用Java语言进行某种抽象的库。 I mean, it could be a "portable" format, with an API for parsing the documents.
我的意思是,它可能是一种“可移植”格式,带有用于解析文档的API。 I guess this could work out with XML, but it would be be a lot more ugly.
我想这可能会解决XML,但它会更难看。
I realize I could just inter-operate with Python, but I don't want to do that. 我意识到我可以与Python互操作,但我不想那样做。
I've looked a lot around and all I could find was Ragel (www.complang.org/ragel), that can also produce Java code. 我看了很多,我能找到的只是Ragel(www.complang.org/ragel),它也可以生成Java代码。 It looked too complex for me so I've started some work to port Construct to Java.
对我来说这看起来太复杂了,所以我开始做一些工作来将Construct移植到Java。 I suspect it would be easier to make something like that in Scala, Groovy or JavaScript.
我怀疑在Scala,Groovy或JavaScript中制作类似的东西会更容易。
Construct on GitHub: https://github.com/MostAwesomeDude/construct 在GitHub上构建: https : //github.com/MostAwesomeDude/construct
java construct: https://github.com/ZiglioNZ/construct java构造: https : //github.com/ZiglioNZ/construct
I've spent a couple of days on it, mostly looking for equivalents of python's expressive classes. 我花了几天时间,主要是寻找python表达类的等价物。 The most useful java classes I've found are: java.util.Scanner, java.util.Formatter and java.nio.ByteBuffer.
我发现的最有用的java类是:java.util.Scanner,java.util.Formatter和java.nio.ByteBuffer。 It's a big task so I want to focus on something small like creating simple parsers and formatters for ByteBuffers.
这是一项很重要的任务,所以我想专注于为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. 我已经移植了足够的代码来解析和构建Python Construct附带的一些协议,例如ethernet,arp和ipv4。 Check it out at https://github.com/ZiglioNZ/construct
请访问https://github.com/ZiglioNZ/construct查看
[Update: new Release] [更新:新版本]
Java Construct 1.1.2 is now available, see release notes . Java Construct 1.1.2现已推出,请参阅发行说明 。
You can use DataInput
/ DataOutput
(and their implementations) to convert any set of values from/to a set of bytes. 您可以使用
DataInput
/ DataOutput
(及其实现)将任何值集合从/转换为一组字节。 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? 这取决于您想要做什么 - 您是否有固定数据格式在线上发送/接收,或者这是否会不时变化?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.