是否有任何用于二进制文件解析的 Java 框架?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/644737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:11:36  来源:igfitidea点击:

Are there any Java Frameworks for binary file parsing?

javaparsingfile-iobinary-data

提问by Kosi2801

My problem is, that I want to parse binary files of different types with a generic parser which is implemented in JAVA. Maybe describing the file format with a configuration file which is read by the parser or creating Java classes which parse the files according to some sort of parsing rules.

我的问题是,我想使用在 JAVA 中实现的通用解析器来解析不同类型的二进制文件。也许用解析器读取的配置文件来描述文件格式,或者创建根据某种解析规则解析文件的 Java 类。

I have searched quite a bit on the internet but found almost nothing on this topic.

我在互联网上搜索了很多,但几乎没有找到关于这个主题的任何内容。

What I have found are just things which deal with compiler-generators (Jay, Cojen, etc.) but I don't think that I can use them to generate something for parsing binary files. But I could be wrong on that assumption.

我发现的只是与编译器生成器(Jay、Cojen 等)有关的东西,但我认为我不能使用它们来生成用于解析二进制文件的东西。但我的假设可能是错误的。

Are there any frameworks which deal especially with easy parsing of binary files or can anyone give me a hint how I could use parser/compiler-generators to do so?

是否有任何框架可以特别处理二进制文件的简单解析,或者任何人都可以给我一个提示,我可以如何使用解析器/编译器生成器来做到这一点?

Update: I'm looking for something where I can write a config-file like

更新:我正在寻找可以编写配置文件的东西,例如

file:
  header: FIXED("MAGIC")
  body: content(10)

content:
  value1: BYTE
  value2: LONG
  value3: STRING(10)

and it generates automatically something which parses files which start with "MAGIC", followed by ten times the content-package (which itself consists of a byte, a long and a 10-byte string).

它会自动生成一些解析以“MAGIC”开头的文件,然后是十倍的内容包(它本身由一个字节、一个长字符串和一个 10 字节字符串组成)。

Update2: I found something comparable what I'm looking for, "Construct", but sadly this is a Python-Framework. Maybe this helps someone to get an idea, what I'm looking for.

更新 2:我发现了一些与我正在寻找的类似的东西,“构造”,但遗憾的是,这是一个 Python 框架。也许这有助于某人得到一个想法,我正在寻找什么。

采纳答案by dfa

give a try to preon

给一个尝试前子

回答by Wilfred Springer

Using Preon:

使用Preon

public class File {

  @BoundString(match="MAGIC")
  private String header;

  @BoundList(size="10", type=Body.class)
  private List<Body> body;

  private static class Body {

    @Bound
    byte value1;

    @Bound
    long value2;

    @BoundString(size="10")
    String value3;

  }


}

Decoding data:

解码数据:

Codec<File> codec = Codecs.create(File.class);
File file = codecs.decode(codec, buffer);

Let me know if you are running into problems.

如果您遇到问题,请告诉我。

回答by Peter Lawrey

I have used DataInputStream for reading binary files and I write the rules in Java. ;) Binary files can have just about any format so there is no general rule for how to read them.

我使用 DataInputStream 读取二进制文件,并用 Java 编写规则。;) 二进制文件几乎可以有任何格式,因此没有关于如何读取它们的一般规则。

Frameworks don't always make things simpler. In your case, the description file is longer than the code to just read the data using a DataInputStream.

框架并不总是让事情变得更简单。在您的情况下,描述文件比仅使用 DataInputStream 读取数据的代码长。

public static void parse(DataInput in) throws IOException {
//        file:
//          header: FIXED("MAGIC")
    String header = readAsString(in, 5);
    assert header.equals("MAGIC");
//          body: content(10)
// ?? not sure what this means
//        content:
    for(int i=0;i<10;i++) {
//          value1: BYTE
        byte value1 = in.readByte();
//          value2: LONG
        long value2 = in.readLong();
//          value3: STRING(10)
        String value3 = readAsString(in, 10);
    }
}

public static String readAsString(DataInput in, int len) throws IOException {
    byte[] bytes = new byte[len];
    in.readFully(bytes);
    return new String(bytes);
}

If you want to have a configuration file you could use a Java Configuration File. http://www.google.co.uk/search?q=java+configuration+file

如果你想要一个配置文件,你可以使用 Java 配置文件。http://www.google.co.uk/search?q=java+configuration+file

回答by Ben Reeves

回答by Igor Maznitsa

I have been developing a framework for Java which allows to parse binary data https://github.com/raydac/java-binary-block-parserin the case you should just describe structure of your binary file in pseudolanguage

我一直在为 Java 开发一个框架,它允许解析二进制数据https://github.com/raydac/java-binary-block-parser如果你应该只用伪语言描述二进制文件的结构

回答by stepancheg

Parser combinator library is an option. JParsecworks fine, however it could be slow.

解析器组合器库是一个选项。JParsec工作正常,但它可能很慢。

回答by asalamon74

You can parse binary files with parsers like JavaCC. Hereyou can find a simple example. Probably it's a bit more difficult than parsing text files.

您可以使用JavaCC 之类的解析器解析二进制文件。在这里你可以找到一个简单的例子。可能比解析文本文件要困难一些。

回答by Milhous

Have you looking into the world of parsers. A good parser is yacc, and there may be a port of it for java.

你有没有研究过解析器的世界。一个好的解析器是yacc,它可能有一个用于 java 的端口。