Are there any Java Frameworks for binary file pars

2019-01-16 20:53发布

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.

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.

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).

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.

8条回答
【Aperson】
2楼-- · 2019-01-16 21:02

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

查看更多
看我几分像从前
3楼-- · 2019-01-16 21:06

Using 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.

查看更多
放荡不羁爱自由
4楼-- · 2019-01-16 21:08
我想做一个坏孩纸
5楼-- · 2019-01-16 21:09

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.

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.

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

查看更多
6楼-- · 2019-01-16 21:15

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

查看更多
唯我独甜
7楼-- · 2019-01-16 21:17

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

查看更多
登录 后发表回答