Android Array Search indexOf(byte[] bytes, byte[] pattern, int start)

Here you can find the source of indexOf(byte[] bytes, byte[] pattern, int start)

Description

Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index.

License

Open Source License

Parameter

Parameter Description
bytes the byte array
pattern the pattern
start the start index from where to search

Return

the index

Declaration

public static int indexOf(byte[] bytes, byte[] pattern, int start) 

Method Source Code

//package com.java2s;
/*//from  www.j  a  va  2  s. com
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group
 */

public class Main {
    /**
     * Calculate the index of the first occurrence of the pattern in the byte array, starting with the given index. This methods returns -1 if the pattern has not been found, and the start position if the pattern is empty.
     * 
     * @param bytes
     *          the byte array
     * @param pattern
     *          the pattern
     * @param start
     *          the start index from where to search
     * @return the index
     */
    public static int indexOf(byte[] bytes, byte[] pattern, int start) {
        if (pattern.length == 0) {
            return start;
        }
        if (start > bytes.length) {
            return -1;
        }
        int last = bytes.length - pattern.length + 1;
        next: for (; start < last; start++) {
            for (int i = 0; i < pattern.length; i++) {
                if (bytes[start + i] != pattern[i]) {
                    continue next;
                }
            }
            return start;
        }
        return -1;
    }
}

Related

  1. inArray(Object o, Object... arr)
  2. indexOf(byte[] data, byte search)
  3. indexof(byte[] shortBytes, byte[] longBytes)