john stamos
john stamos

Reputation: 1124

Java I want to search for a binary hex value in my byte array

public static void main(String[] args) {
  File inFile = null;
  if (0 < args.length) {
      inFile = new File(args[0]);
  }

    BufferedInputStream bStream = null;

    try {

        int read;
        byte[] bytes = new byte[1260];
        bStream = new BufferedInputStream(new FileInputStream(inFile));

        while ((read = bStream.read(bytes)) > 0) {
            getMarker(read);
        }
    }

 private static void getMarker(int read) {

 }

I'm having trouble seeing where my byte array is created and where I can access the data in the byte array. I thought read would have been my byte array where I could search for my marker in getMarker (probably using a long), but read is just an integer value. Is the data in my byte array in bytes then? Or where can I access the actually binary values in the byte array to search?

Upvotes: 0

Views: 773

Answers (1)

Joni
Joni

Reputation: 111279

The read methods fills a part of the array you pass with data read from the file, and returns the number of bytes it read. To access the array in your getMarker method you have to pass it there, not just the number of bytes that were read into it. For example:

    while ((read = bStream.read(bytes)) > 0) {
        getMarker(read, bytes);
    }
...

 private static void getMarker(int read, byte[] bytes) {
     // data has been read into the "bytes" array into index 0 through read-1
 }

Upvotes: 2

Related Questions