private static long scanForSequence ( final ImageInputStream pStream , final byte [ ] pSequence ) throws IOException { long start = - 1l ; int index = 0 ; int nullBytes = 0 ; for ( int read ; ( read = pStream . read ( ) ) >= 0 ; ) { if ( pSequence [ index ] == ( byte ) read ) { // If this is the first byte in the sequence, store position if ( start == - 1 ) { start = pStream . getStreamPosition ( ) - 1 ; } // Inside the sequence, there might be 1 or 3 null bytes, depending on 16/32 byte encoding if ( nullBytes == 1 || nullBytes == 3 ) { pStream . skipBytes ( nullBytes ) ; } index ++ ; // If we found the entire sequence, we're done, return start position if ( index == pSequence . length ) { return start ; } } else if ( index == 1 && read == 0 && nullBytes < 3 ) { // Skip 1 or 3 null bytes for 16/32 bit encoding nullBytes ++ ; } else if ( index != 0 ) { // Start over index = 0 ; start = - 1 ; nullBytes = 0 ; } } return - 1l ; }
Scans for a given ASCII sequence .