Best way to read structured binary files with Java

前端 未结 12 887
我寻月下人不归
我寻月下人不归 2020-12-04 13:23

I have to read a binary file in a legacy format with Java.

In a nutshell the file has a header consisting of several integers, bytes and fixed-length char arrays, f

相关标签:
12条回答
  • 2020-12-04 13:49

    I guess FileInputStream lets you read in bytes. So, opening the file with FileInputStream and read in the sizeof(header). I am assuming that the header has a fixed format and size. I don't see that mentioned in the initial post, but assuming that is the case as it would get much more complex if the header has optional args and different sizes.

    Once you have the info, there can be a header class in which you assign the contents of the buffer that you've already read. And then parse the records in a similar fashion.

    0 讨论(0)
  • 2020-12-04 13:50

    A while ago I found this article on using reflection and parsing to read binary data. In this case, the author is using reflection to read the java binary .class files. But if you are reading the data into a class file, it may be of some help.

    0 讨论(0)
  • 2020-12-04 13:51

    I would create an object that wraps around a ByteBuffer representation of the data and provide getters to read directly from the buffer. In this way, you avoid copying data from the buffer to primitive types. Furthermore, you could use a MappedByteBuffer to get the byte buffer. If your binary data is complex, you can model it using classes and give each class a sliced version of your buffer.

    class SomeHeader {
        private final ByteBuffer buf;
        SomeHeader( ByteBuffer fileBuffer){
           // you may need to set limits accordingly before
           // fileBuffer.limit(...)
           this.buf = fileBuffer.slice();
           // you may need to skip the sliced region
           // fileBuffer.position(endPos)
        }
        public short getVersion(){
            return buf.getShort(POSITION_OF_VERSION_IN_BUFFER);
        }
    }
    

    Also useful are the methods for reading unsigned values from byte buffers.

    HTH

    0 讨论(0)
  • 2020-12-04 13:54

    As other people mention DataInputStream and Buffers are probably the low-level API's you are after for dealing with binary data in java.

    However you probably want something like Construct (wiki page has good examples too: http://en.wikipedia.org/wiki/Construct_(python_library), but for Java.

    I don't know of any (Java versions) off hand, but taking that approach (declaratively specifying the struct in code) would probably be the right way to go. With a suitable fluent interface in Java it would probably be quite similar to a DSL.

    EDIT: bit of googling reveals this:

    http://javolution.org/api/javolution/io/Struct.html

    Which might be the kind of thing you are looking for. I have no idea whether it works or is any good, but it looks like a sensible place to start.

    0 讨论(0)
  • 2020-12-04 13:55

    If you would be using Preon, then all you would have to do is this:

    public class Header {
        @BoundNumber int version;
        @BoundNumber byte type;
        @BoundNumber int beginOfData;
        @BoundString(size="15") String id;
    }
    

    Once you have this, you create Codec using a single line:

    Codec<Header> codec = Codecs.create(Header.class);
    

    And you use the Codec like this:

    Header header = Codecs.decode(codec, file);
    
    0 讨论(0)
  • 2020-12-04 13:56

    Here is a link to read byte using a ByteBuffer (Java NIO)

    http://exampledepot.com/egs/java.nio/ReadChannel.html

    0 讨论(0)
提交回复
热议问题