public static int search()

in axiom-compat/src/main/java/org/apache/axiom/attachments/utils/ByteSearch.java [179:230]


    public static int search(byte[] search, 
            byte[] bytes, int start, int end, 
            boolean direction) {
        
        int idx = -1;
        if (search == null || search.length == 0 ||
            bytes == null || bytes.length == 0 ||
            start < 0 || end <= 0) {
            return idx;
        }
        
        // Search byte bytes array
        if (direction) {
            for (int i=start; idx < 0 && i< end; i++) {
                if (bytes[i] == search[0]) {
                    // Potential match..check remaining bytes
                    boolean found = true;  // assume found
                    for (int i2=1; found && i2<search.length; i2++) {
                        if (i+i2 >= end) {
                            found = false;
                        } else {
                            found = (bytes[i+i2] == search[i2]); 
                        }
                    }
                    // If found match, set return idx
                    if (found) {
                        idx = i;
                    }
                }
            }
        } else {
            for (int i=end-1; idx < 0 && i>=start; i--) {
                if (bytes[i] == search[0]) {
                    // Potential match..check remaining bytes
                    boolean found = true;  // assume found
                    for (int i2=1; found && i2<search.length; i2++) {
                        if (i+i2 >= end) {
                            found = false;
                        } else {
                            found = (bytes[i+i2] == search[i2]); 
                        }
                    }
                    // If found match, set return idx
                    if (found) {
                        idx = i;
                    }
                }
            }
        }

        return idx;
    }