Android Byte Array Search search(byte[] array, int start, int count, byte value)

Here you can find the source of search(byte[] array, int start, int count, byte value)

Description

Search for a value in the array.

Parameter

Parameter Description
array The array to be searched.
start Index to start the search.
count Number of bytes to search in \a array. When less than zero or greater than the length of the array starting at \a start all the data will be searched. No exceptions thrown.
value Value to be searched in the array \a array.

Return

The index of the element when \a value is found. Otherwise -1 is returned. -1 is also returned when \a array is \b null, \a start is less than zero or greater than \a count.

Declaration

public static int search(byte[] array, int start, int count, byte value) 

Method Source Code

//package com.java2s;

public class Main {
    /**/* ww  w.  j a  v a2s.com*/
     * Search for a value in the array.
     * @param array The array to be searched.
     * @param start Index to start the search.
     * @param count Number of bytes to search in \a array. When less than zero
     * or greater than the length of the array starting at \a start all the
     * data will be searched. No exceptions thrown.
     * @param value Value to be searched in the array \a array.
     * @return The index of the element when \a value is found. Otherwise -1
     * is returned. -1 is also returned when \a array is \b null, \a start is
     * less than zero or greater than \a count.
     **/
    public static int search(byte[] array, int start, int count, byte value) {
        if ((array == null) || (array.length <= start) || (start < 0))
            return -1;

        if ((count < 0) || (count > (array.length - start)))
            count = array.length - start;

        for (int i = 0; i < count; i++) {
            if (array[start + i] == value)
                return (start + i);
        }
        return -1;
    }

    /**
     * \copydoc search(byte[],int,int,byte)
     **/
    public static int search(byte[] array, int start, int count, short value) {
        if ((array == null) || (array.length <= start) || (start < 0))
            return -1;

        if ((count < 0) || (count > (array.length - start)))
            count = array.length - start;

        for (int i = 0; i < count - 1; i++) {
            if (value == bigEndToShort(array, (start + i)))
                return (start + i);
        }
        return -1;
    }

    /**
     * Converts a byte array into short from big-endian format.
     * \param array The byte array to convert from.
     * \param start The starting point, int the array \a a, where the
     * conversion should start.
     * \returns The number after the conversion.
     **/
    public static short bigEndToShort(byte[] array, int start) {
        return (short) ((array[start + 0] << 8) | (array[start + 1] & 0xFF));
    }
}

Related

  1. search(byte[] array, int start, int count, short value)
  2. search(byte[] needle, byte[] haystack, int from, int to)