public int indexOf ( byte [ ] bytes , int offset ) { // make sure we're not checking against any byte arrays of length 0 if ( bytes . length == 0 || size ( ) == 0 ) { // a match is not possible return - 1 ; } // make sure the offset won't cause us to read past the end of the byte[] if ( offset < 0 || offset >= size ( ) ) { throw new IllegalArgumentException ( "Offset must be a value between 0 and " + size ( ) + " (current buffer size)" ) ; } int length = size ( ) - bytes . length ; for ( int i = offset ; i <= length ; i ++ ) { int j = 0 ; // continue search loop while the next bytes match while ( j < bytes . length && getUnchecked ( i + j ) == bytes [ j ] ) { j ++ ; } // if we found it, then j will equal the length of the search bytes if ( j == bytes . length ) { return i ; } } // if we get here then we didn't find it return - 1 ; }
Returns the index within this buffer of the first occurrence of the specified bytes after the offset . The bytes to search for must have a size lower than or equal to the current buffer size . This method will return - 1 if the bytes are not contained within this byte buffer .