3

My code below only parses through the data file once. I'm trying to get it to parse through the whole file. Every time it finds a marker, parse the data and append it to the output file. Currently it successfully parses the data once and then stops. Can't figure out how to keep it looping until eof. The data is 4 byte aligned and is in a input binary file.

 private static void startParse(File inFile) throws IOException {
      boolean markerFound = false;
      for (int offset = 0; !markerFound && offset < 4; offset++){
         DataInputStream dis = new DataInputStream(new FileInputStream(inFile));
         for (int i = 0; i < offset; i++){
             dis.read();
         }
         try {
             int integer;
             long l;
             while((l = (integer = dis.readInt())) != MARKER) {
                 //Don't do anything
             }
             markerFound = true;
             for (int i = 0; i < 11; i++){
                 dis.read();
             }

     // ********************** data **********************          
             byte[] data = new byte[1016];
             for(int i = 0; i < 1016; i++){
             data[i] = (byte) dis.read();
             }

             for (int i = 0; i < 4; i++){
                 dis.read();
             }

     // ***************** output data ********************
             if (checksumCheck(checksum) && fecfCheck(fecf)){
                 FileOutputStream output = new FileOutputStream("ParsedData", true);
            try{
                output.write(data);
            } 
            finally{
                output.close();
            }
        }

    }               
    catch (EOFException eof) {
    }
    dis.close();
 }
 }
8
  • You mean it does not parse until the end of the data?
    – fge
    Jun 6, 2013 at 17:27
  • BTW, I suggest you use FileChannel.map() for this kind of job
    – fge
    Jun 6, 2013 at 17:28
  • Yes, there is a section of data inside the file, and it only parses the first marker section of data and there is about 50 in the file. And I said 'data' as the rest is just garbage I don't care about and am parsing out the actual 'data' I want. Jun 6, 2013 at 17:30
  • Are you sure the marker is present, 4-byte aligned and of correct byte order (little/big endian)? markerFound = integer == MARKER;
    – Joop Eggen
    Jun 6, 2013 at 17:34
  • Please include a description of the file format. Your question doesn't make much sense without it.
    – user207421
    Jun 10, 2013 at 22:56

3 Answers 3

6
markerFound = true;

This line is not inside a conditional and will be executed in any occurrence of the loop.
Which will of course shut down your loop because:

for (int offset = 0; !markerFound && offset < 4; offset++)
1
  • But it can only exit the while loop above it if it has found the marker. Otherwise it will get an eof exception. If this is not the case then I may need a further explanation as I would then be confused. Jun 6, 2013 at 17:41
1

First thing

You are opening the file inside your for, so, the reading always will start at the beginning of the file. Open it before the first for.

Second

Because of the test !markerFound && offset < 4, your loop will occur max 4 times.

Third

This code not make sense to me:

for (int i = 0; i < offset; i++){
    dis.read();
}

Because the offset, in the first iteration, is 0, in the next will be 1, and so on. And that loop is not necessary, you are using another loop to read bytes until reach the MARKER.

Fourth

If your file has "records" with fixed lenghts and the markers occurs on predictable positionings, use the DataInputStream skipBytes method to go forward to next marker.

0

As I'd posted in an earlier answer to your question Java, need a while loop to reach eof. i.e.while !eof, keep parsing I'd like to state again that DataInputStream.read() (unlike other readXxX() methods) does not throw EOFExcepion.

From the JavaDocs: (DataInputStream inherits read() from FilterInputStream)

If no byte is available because the end of the stream has been reached, the value -1 is returned.

So, to correctly check for EOF, usually read(byte[]) is used in a while loop as follows:

int read = 0;
byte[] b = new byte[1024];
while ((read = dis.read(b)) != -1) { // returns numOfBytesRead or -1 at EOF
  // fos = FileOutputStream
  fos.write(b, 0, read); // (byte[], offset, numOfBytesToWrite)
}

Answer

Now, getting back to your current question; since, you haven't shared your binary file format it's difficult to suggest a better way to parse it. So, from the limited understanding of the way your nested loops are parsing your file currently; you need another while loop (as reasoned above) to read/parse and copy your "data" till you reach EOF once you've found the marker.

markerFound = true;
for (int i = 0; i < 11; i++){ // move this loop inside while IF
    dis.read(); // these 11 bytes need to be skipped every time
}

// Open the file just ONCE (outside the loop)
FileOutputStream output = new FileOutputStream("ParsedData", true);

// ********************** data **********************
int read = 0;
byte[] data = new byte[1016]; // set byte buffer size

while ((read = dis.read(data)) != -1) { // read and check for EOF

    // ***************** output data ********************
    if (checksumCheck(checksum) && fecfCheck(fecf)) { // if checksum is valid
        output.write(data, 0, read); // write the number of bytes read before
    }

    // SKIP four bytes
    for (int i = 0; i < 4; i++) { // or, dis.skipBytes(4); instead of the loop
        dis.read();
    }
 }

// Close the file AFTER input stream reaches EOF
output.close(); // i.e. all the data has been written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.