Here you can find the source of indexOf(byte[] array, byte[] target, int fromIndex)
Parameter | Description |
---|---|
array | the array to search for the sequence target |
target | the array to search for as a sub-sequence of array |
fromIndex | the index to start the search from in array |
public static int indexOf(byte[] array, byte[] target, int fromIndex)
//package com.java2s; /*/* w w w .j a v a 2 s . c o m*/ Copyright 2012 Twitter, Inc. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ public class Main { /** * Returns the index (start position) of the first occurrence of the specified * {@code target} within {@code array} starting at {@code fromIndex} , or * {@code -1} if there is no such occurrence. * * <p> * Returns the lowest index {@code k} such that {@code k >= fromIndex} and * {@code java.util.Arrays.copyOfRange(array, k, k + 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} * @param fromIndex * the index to start the search from in {@code array} */ public static int indexOf(byte[] array, byte[] target, int fromIndex) { if (array == null || target == null) { return -1; } // Target cannot be beyond array boundaries if (fromIndex < 0 || (fromIndex > (array.length - target.length))) { return -1; } // Empty is assumed to be at the fromIndex of any non-null array (after // boundary check) if (target.length == 0) { return fromIndex; } firstbyte: for (int i = fromIndex; i < array.length - target.length + 1; i++) { for (int j = 0; j < target.length; j++) { if (array[i + j] != target[j]) { continue firstbyte; } } return i; } return -1; } }