permaculture playing cards*
The moose likes Java in General and the fly likes Manipulating a Binary file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Manipulating a Binary file" Watch "Manipulating a Binary file" New topic
Author

Manipulating a Binary file

Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
Hello all,

I have a binary file and the corresponding file of schema. The order of schema is (char, int, double). I was wondering how I would go displaying the binary file according to the schema. I know how to read in the binary file as a series of bytes, but where would i go from there? For example: how would i simply print out the first three variables?
Anayonkar Shivalkar
Bartender

Joined: Dec 08, 2010
Posts: 1502
    
    5

Ryan Callen wrote:Hello all,

I have a binary file and the corresponding file of schema. The order of schema is (char, int, double). I was wondering how I would go displaying the binary file according to the schema. I know how to read in the binary file as a series of bytes, but where would i go from there? For example: how would i simply print out the first three variables?

Well, if you are aware of dealing with bytes, then all you'll need to do is - refer the schema file and decode accordingly (e.g. first n bytes are for length, next k bytes are for datatype, and so on).

I hope this answers your question.


Regards,
Anayonkar Shivalkar (SCJP, SCWCD, OCMJD, OCEEJBD)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Anayonkar Shivalkar wrote:and decode accordingly

And a good way to do that is using a DataInputStream
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Stuart A. Burkett wrote:
Anayonkar Shivalkar wrote:and decode accordingly

And a good way to do that is using a DataInputStream


Note that a DataInputStream would only work if the binary file was written with bytes in the Java standard order and variable size - for example, in the double field.

IF you get strange results, you may have to do some byte manipulation to make Java happy.

Bill
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
Okay. I read it in using data input stream, but the issue I'm having now is knowing how long the file is. For example, I have a while loop that reads the bytes in and converts them to schema, but I'm not sure what condition to place on the while loop in order to make it stop at the appropriate line. Datainputstream doesn't have a .hasNext() method.
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
All the read methods throw an EOFException if there are no bytes left, so you just keep reading until this is thrown.
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
So there is nothing to count it with (aside from bytes)?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I think the idea behind the streams the deal with higher level abstractions--DatatInput/OutputStream and ObjectInput/OutputStream is that you define the format of your file and you know how much is in there. You shouldn't have to rely on the exception to tell you when you've hit the end.

For instance, the simplest, most direct approach is to just write out one full object graph with a single call to ObjectOutputStream.writeObject(), and read it in with a single call to readObject().

In your case, it sounds like you have {char,int,double} repeated some number of times? I can see several approaches, but they all require you to change the format of the file. If you can't do that, you're stuck either reading bytes, creating the char/int/double values yourself from the bytes, and watching for a -1 as a read() result; or else just using the exception to detect EOF (an icky approach, to be avoided if possible).

Anyway, some suggestions:

1. First write out an int or a long telling you how many {char,int,double} sequences there will be. When reading the file, read that count first, and then just loop that many times, reading one {c,i,d} each time.

2. If there's a particular value that can never occur for one element of that triplet, or for the triplet as a whole, write that out as an EOF marker. E.g. {0,Integer.MIN_VALUE,Double.NEGATIVE_INFINITY}

3. Define a class that holds {c,i,d} as member variables, and declare it Serialzable. Put the values into instances of that class, and put the instances into a List, such as an ArrayList. Use a single call to ObjectOutputStream.writeObject() to write the list out, and a single call to ObjectInputStream.read() to read the List back in.
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
If I know that my schema file is in the format of {char, int, double}, how do I make java recognize this? In other words, how can I make java recognize the format of the schema file without hardcoding the variables in? - in case i change it
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Callen wrote:If I know that my schema file is in the format of {char, int, double}, how do I make java recognize this? In other words, how can I make java recognize the format of the schema file without hardcoding the variables in? - in case i change it


Java doesn't know. You have to tell it. So, in the simplest case, your code would be


If you want your app do be able to handle different formats, you need to define what the formats can be, define a way to detect which format it is, and then wirte code that detects and parses the format accordingly.

It's not possible to write a program to just automatically recognize and parse any arbitrary format.
Ryan Callen
Greenhorn

Joined: Nov 29, 2012
Posts: 22
The method you listed above is the one I am currently using. When I hardcode the variables from the schema in, I have no issues. But I'm just not sure how I can detect which variables are in the schema file, in case it changes. For example, if I were to let the user choose their own custom schema file, how could I have java detect which variables they are using?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Ryan Callen wrote:I'm just not sure how I can detect which variables are in the schema file, in case it changes.


As I stated above, you can't, unless you define a way to do so. If today you're getting {char,int,double} and then tomorrow the sender instead decides to send {double,long,char,int} and doesn't tell you, there is no possible way for you to tell that just from looking at the file, unless there's metadata in that file indicating the format.

If you can't get that metadata inserted into the file, then the sender will have to tell you what's changed and you'll have to change you code accordingly. If it's not practical to change your code that much, you can create somewhat more complex but flexible code that can, for instance, read the format from a config file, and then you just have to change the config file when the format changes.

I've already given you some suggestions, but you haven't commented on them or provided any more information about your requirements, constraints, and use cases. If you're just going to keep repeating, "How can Java detect when the file format changes?" you're going to get precisely nowhere.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Here are some bits. Tell me what they represent:
1101000011010111111100111011110000101010000100010011010111001010

If you can do that, then whatever magic you used, apply it to your problem of automatically determining a file's format just from its contents.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Manipulating a Binary file
 
Similar Threads
No target namespace
How do I create an XML schema?
Binary Attachments
How to read binary file?
Converting a DTO object to an XML File