Here you can find the source of indexOf(byte[] array, byte[] target, int start)
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.
Parameter | Description |
---|---|
array | the array to search for the sequence target |
target | the array to search for as a sub-sequence of array |
public static int indexOf(byte[] array, byte[] target, int start)
//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; } }