Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there any Java Frameworks for binary file parsing?

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.

like image 346
Kosi2801 Avatar asked Mar 13 '09 21:03

Kosi2801


People also ask

How does Java handle binary data?

A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.

Is a Java file a binary file?

For example, the Java source programs are stored in text files and can be read by a text editor, but the Java classes are stored in binary files and are read by the JVM.


2 Answers

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.

like image 157
Wilfred Springer Avatar answered Sep 20 '22 11:09

Wilfred Springer


give a try to preon

like image 20
dfa Avatar answered Sep 17 '22 11:09

dfa