views:

1982

answers:

7

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.

+7  A: 

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

Peter Lawrey
+1: No framework. No easy parsing.
S.Lott
I know that binary files can have any format, but a PNG-file has always the same structure, a BMP has always the same structure...What I want is: create a "description" for a eg. BMP file, feed it to the framework, feed a BMP and then have easy access to each single element of the parsed content.
Kosi2801
Kosi2801: If you can write specification or just a serie of "how it really works" steps for such framework, then it's doable. If you can't, it's not.
Esko
@Perter Lawrey: In that simple case you're right, the config is longer. But my goal is to be able to deliver something to the client where I can provide additional configs later to support additional file formats. Think of parsing image files where you cannot update the binary for the customer...
Kosi2801
@Kosi2801 I am not sure why you feel providing a text file is any easier than providing a class or jar file. If you need to provide the update as text you can use BeanShell, apache-jci or the builtin Compiler API to compile/load java code on the fly.
Peter Lawrey
Nice example, but I'd strongly discourage calling that String-constructor! Always define a character set, or it will come back to bite you in the behind ...
Joachim Sauer
With your update you've lined out a solution where the structure of the file to read is hardcoded in the source. What I need is, that the config-file describes the structure of the file and the parser reads it in in an easy-accessible way. Changes only happen in the config file...
Kosi2801
You can deploy a java file as a config file. There is no difference except the format you use. What is the difference between a config file written in text and a java file written in text. One is neither more hard coded that the other.
Peter Lawrey
Tried to keep business out as long as possible, but: With the software the customer has to be able to provide a description of the file-format to the app without the need to learn Java and have to deal with all of our (to be done then) interfaces. Just let him "describe" his file-format and done...
Kosi2801
So instead of learning a sub-set of java (which they might know or be able to get help off the web for) they have to learn a new language which is unlikely to describe all the situations they will need which they definitely won't know and won't have much incentive to invest much time in.
Peter Lawrey
Exactly. :) If the customer orders it and doesn't want to be convinced, we try to deliver. I also think it's safer and easier to provide him with 2 pages of description and a restricted language instead of allowing him to fiddle him around in the guts of the application and doing all sorts of stuff.
Kosi2801
This reason is valid. In this case I would suggest something which has a simple mapping to what you would do in Java. Note: you may have to support loops and conditional logic which is not simple. You may find that you introduce more bugs than you solve.
Peter Lawrey
Kosi2801
A: 

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

Milhous
A: 

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.

asalamon74
+1  A: 

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

stepancheg
+2  A: 

Google's Protocol Buffers

Ben Reeves
Had a short look at it but it seems not to provide bit-level access to the data stream content but encapsulates it somehow (to provide optionality etc.). My assumption is also backed because it seems to be necessary to use the Protocol Buffers on both ends of the comm channel.
Kosi2801
+3  A: 

give a try to preon

dfa
That's it. Thank you!
Kosi2801
+3  A: 

In Preon, it would be like this:

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;

  }


}

And you would decode data like this:

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

Let me know if you are running into problems.

Wilfred Springer