0
  • I have a GB file of binary data.
  • In this file are many sections of data.
  • Each section of data is wrapped in crap that I don't want.
  • In the crap that I don't want there are constant indicator markers that tell me when the data is coming up.
  • I read a number of bytes, read the crap that I don't want in order to find a section of data, then parse only the data to an output file.

So far I have accomplished this. Except it only parses once, one section, then stops. The program is still running and the output file only has one section of parsed data in it. So it is hung up. I need the program to continue from its last section, looking for the next marker and then parse the next section and continue to do so until eof. Here is my code:

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

     // ***************** checksum value *****************
             byte[] checksum = new byte[2];
             checksum[0] = (byte) dis.read();
             checksum[1]    = (byte) 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();
             }

     // ********************** fecf **********************
             byte[] fecf = new byte[2];
             fecf[0] = (byte) dis.read();
             fecf[1] = (byte) dis.read();

     // ***************** output data ********************
             if (checksumCheck(checksum) && fecfCheck(fecf)){
                 FileOutputStream output = new FileOutputStream("ParsedData", true);
                 try{
                     output.write(data);
                 } 
                 finally{
                     output.close();
                 }
             }
             else {
                 FileOutputStream output = new FileOutputStream("ParsedData", true);
                 try{
                     output.write(36606); //TODO: Change value to bad data flag.
                 }
                 finally{
                     output.close();
                 }
             }

         }              
         catch (EOFException eof) {
             System.out.println("catch");
             endOfFile = true;
         }
     }
 dis.close();
 }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
john stamos
  • 1,054
  • 5
  • 17
  • 36

1 Answers1

1

dis.close() will close the underlying FileInputStream, hence you only read one section. move the DataInputStream open and close outside of the while loop (and put all your close calls in finally blocks while you are at it).

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • Awesome. It now looks to parse to the end of the file. Only it looks to be outputting the first data section repeatedly, instead of the next data's section. I have updated my code with dataInputStream dis =... and dis.close() outside of the while loop. – john stamos Jun 26 '13 at 19:50
  • @johnstamos - as a side note, `dis.readFully(byte[])` would simplify your code dramatically. – jtahlborn Jun 26 '13 at 20:26
  • @johnstamos - next step is probably to step through the code with a debugger. nothing seems out of place at this point in the code. – jtahlborn Jun 26 '13 at 20:29