1

My app reads a png-like file (in byte) and stores bytes into a byteArray. This is the method I use :

public static byte[] read(File file) throws IOException {

        byte []buffer = new byte[(int) file.length()];
        InputStream ios = null;
        try {
            ios = new FileInputStream(file);
            if ( ios.read(buffer) == -1 ) {
                throw new IOException("EOF reached while trying to read the whole file");
            }        
        } finally { 
            try {
                 if ( ios != null ) 
                      ios.close();
            } catch ( IOException e) {
            }
        }

        return buffer;
    }

After that, I want to extract patterns of that byteArray.

It follows the pattern of png file :
4 bytes length + 4 bytes types + data (optional) + CRC and repeats that scheme.

I want to do something like a do while : reading the length + type. If I'm not interested of the type, I want to skip that chunk. But I'm struggling because I can't find any skip method for byteArray[].

Does anyone have an idea of how to proceed ?

tmylamoule
  • 161
  • 1
  • 12

2 Answers2

2

Have you tried to use ByteArrayInputStream http://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html? there is skip method

ppysz
  • 380
  • 5
  • 21
  • I saw this Class earlier. But I don't know how to use it. Do I have to replace my FileInputStream to my ByteArrayInputStream ? – tmylamoule Jun 15 '15 at 11:37
  • If there is nothing crucial for you in FileInputStream you can just replace it, both of them has the same superclass. Is that enough example for you http://www.tutorialspoint.com/java/io/bytearrayinputstream_skip.htm? – ppysz Jun 15 '15 at 11:45
  • Yes, Thank you for the help. I'm working on it. Will give you a feedback ! ;) – tmylamoule Jun 15 '15 at 11:59
  • I finally manage to make it work way faster. (before : 7 sec, now : 0,4 seconds) I've only one little question now : I make a do while with this condition : if chunk is end_chunk then it's over. Things work great but if it's a corrupted file (with no end), that will loop and don't stop since my file has no end chunk. How can I surpass that ? Many thanks to you ! – tmylamoule Jun 15 '15 at 14:00
  • http://stackoverflow.com/questions/2295221/java-net-url-read-stream-to-byte maybe this will help you :) I'm glad that I've helped you so far :) – ppysz Jun 15 '15 at 17:48
  • I'm in a do {....... switch{....} }while. How can I get out of my do while if I am in a switch case ? – tmylamoule Jun 16 '15 at 08:32
  • You can't get out of switch because nothing fits ? – ppysz Jun 16 '15 at 08:38
  • Hey, thanks again for the answer. I spoke too fast. My while condition was bad. Now everything's working! Thanks ! – tmylamoule Jun 16 '15 at 08:48
0

If you want to iterate through the array with a while and you need to skip to the next iteration on a given condition you can use the label continue to skip to the next iteration of the loop.

The syntax is as follows:

do {
    if (condition) {
        continue;
    }
    // more code here that will only run if the condition is false
} while(whatever you use to iterate over your array);
Nagarz
  • 178
  • 2
  • 13
  • That's my idea. But I don't know how to fill the white arguments. For the moment I've made a "for loop" that increments bytes per bytes and searching for the 4-bytes TYPE. But this loop is so slow : 7 seconds for a 20Mb file. (I have 250Mb file to read) – tmylamoule Jun 15 '15 at 11:42