Java Array Index Of indexOf(byte[] array, byte[] target, int start)

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

Description

origin: https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/primitives/Bytes.java Returns the start position of the first occurrence of the specified target within array , or -1 if there is no such occurrence.

License

Open Source License

Parameter

Parameter Description
array the array to search for the sequence target
target the array to search for as a sub-sequence of array

Declaration

public static int indexOf(byte[] array, byte[] target, int start) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /** origin: https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/primitives/Bytes.java
     * Returns the start position of the first occurrence of the specified {@code
     * target} within {@code array}, or {@code -1} if there is no such occurrence.
     *//  w  w w .  j  ava2 s.c o m
     * <p>More formally, returns the lowest index {@code i} such that {@code
     * java.util.Arrays.copyOfRange(array, i, i + target.length)} contains exactly
     * the same elements as {@code target}.
     *
     * @param array the array to search for the sequence {@code target}
     * @param target the array to search for as a sub-sequence of {@code array}
     */
    public static int indexOf(byte[] array, byte[] target, int start) {
        checkNotNull(array, "array");
        checkNotNull(target, "target");
        if (target.length == 0) {
            return 0;
        }

        outer: for (int i = start; i < array.length - target.length + 1; i++) {
            for (int j = 0; j < target.length; j++) {
                if (array[i + j] != target[j]) {
                    continue outer;
                }
            }
            return i;
        }
        return -1;
    }

    /** origin: https://code.google.com/p/guava-libraries/source/browse/guava/src/com/google/common/base/Preconditions.java
     * Ensures that an object reference passed as a parameter to the calling method is not null.
     *
     * @param reference an object reference
     * @param errorMessage the exception message to use if the check fails; will be converted to a
     *     string using {@link String#valueOf(Object)}
     * @return the non-null reference that was validated
     * @throws NullPointerException if {@code reference} is null
     */
    public static <T> T checkNotNull(T reference, /*@Nullable*/
            Object errorMessage) {
        if (reference == null) {
            throw new NullPointerException(String.valueOf(errorMessage));
        }
        return reference;
    }
}

Related

  1. arrayIndexOf(int needle, int[] haystack)
  2. arrayIndexOf(int value, int[] arr)
  3. arrayIndexOf(Object[] array, Object element)
  4. arrayIndexOffset(int index, long baseOffset, long indexScale)
  5. indexOf(byte[] array, byte[] target, int fromIndex)
  6. indexOf(byte[] target, byte[] key)
  7. indexOf(char toBeFound, char[] array)
  8. indexOf(final String[] p_elements, final String p_key)
  9. indexOf(final T[] array, final T value)