3
public static void main(String[] args) {
   File inFile = null;
   if (0 < args.length) {
      inFile = new File(args[0]);
   }
   BufferedInputStream bStream = null;
   try {
      int read;
      bStream = new BufferedInputStream(new FileInputStream(inFile));
      while ((read = bStream.read()) > 0) {
         getMarker(read, bStream);
         System.out.println(read);
      }
   }
   catch (IOException e) {
      e.printStackTrace();
   }
   finally {
      try {
         if (bStream != null)bStream.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

private static void getMarker(int read, BufferedInputStream bStream) {
}

I want to find the long 1234567890 in the bufferedInputStream. Am I able to search the bufferedInputStream for a long type? (I'm not sure whether I need 'read' as a parameter. I doubt it, I may remove that). How do I search a bufferedInputStream? Big endian, 8 byte aligned.

The initial marker that I'm searching for contains the value 1234567890. Once I have found that value I want to put the value of 2 bytes into a variable. These 2 bytes are located 11 bytes after marker.

Aubin
  • 14,617
  • 9
  • 61
  • 84
john stamos
  • 1,054
  • 5
  • 17
  • 36

1 Answers1

2

With the method java.io.DataInputStream.readLong() it's possible to read data 8 bytes per 8 bytes. But the question is: the file contains only long or other data?

If the data may be anywhere, we have to read 8 times the file beginning at the offset 0, 1, 2 and so on.

class FuzzyReaderHelper {

   public static final long MAGIC_NUMBER = 1234567890L;

   public static DataInputStream getStream( File source ) {
      boolean magicNumberFound = false;
      for( int offset = 0; !magicNumberFound && offset < 8; ++offset ) {
         dis = new DataInputStream( new FileInputStream( source ));
         for( int i = 0; i < offset; ++i ) {
            dis.read();
         }
         try {
            long l;
            while(( l = dis.readLong()) != MAGIC_NUMBER ) {
               /* Nothing to do... */
            }
            magicNumberFound = true;
            for( int i = 0; i < 11; ++i ) {
               dis.read();
            }
            return dis;
         }
         catch( EOFException eof ){}
         dis.close();
      }
   // choose:
      throw new IllegalStateException( "Incompatible file: " + source );
   // or
      return null; 
   }
}

The next steps are up to you:

DataInputStream dis = FuzzyReaderHelper.getStream( new File( root, "toto.dat" ));
if( dis != null ) {
   byte[] bytes = new byte[2];
   bytes[0] = dis.read();
   bytes[1] = dis.read();
   ...
}
Aubin
  • 14,617
  • 9
  • 61
  • 84
  • The initial marker that I'm searching for contains the value 1234567890. Once I have found that value I want to put the value of 2 bytes into a variable. These 2 bytes are located 11 bytes after marker. – john stamos Jun 03 '13 at 18:39
  • Now what if there was another 1000 bytes after the retval. How could we put those 1000 bytes to a variable using a separate method? – john stamos Jun 04 '13 at 16:31
  • I guess my question is, how do I continue on in the 'dis' from where I left off? My thoughts are that I may have to pull the 'dis' out of this method and have it in my main function. Then call a separate 'retVal' function and a separate 'thousandBytes' function? Is this possible? – john stamos Jun 04 '13 at 16:42
  • What happens at eof? If there are no markers left wouldn't it get stuck in the while loop? Never mind, That's why it's a try catch end of file. – john stamos Jun 04 '13 at 19:02
  • http://stackoverflow.com/questions/664331/when-will-an-eofexception-occur-in-javas-streams – Aubin Jun 04 '13 at 19:05
  • I think we would have to cast dis.read(); as a byte, correct?ie. bytes[0] = (byte) dis.read(); – john stamos Jun 04 '13 at 21:07
  • Okay, I was having some trouble with the while loop as it wasn't finding my marker (marker is the first thing in the file among other places). The file is actually 4 byte aligned(my bad), so I changed the offset to 4. What I want to do is readInt instead of read long, then store that int in a long in order to not worry about signed or unsigned. – john stamos Jun 04 '13 at 21:33