Retrieve a byte from a byte array. - Java java.lang

Java examples for java.lang:byte Array

Description

Retrieve a byte from a byte array.

Demo Code

/* /*w  w  w .j  a v  a 2  s  . com*/
 * Licensed to Aduna under one or more contributor license agreements.  
 * See the NOTICE.txt file distributed with this work for additional 
 * information regarding copyright ownership. 
 *
 * Aduna licenses this file to you under the terms of the Aduna BSD 
 * License (the "License"); you may not use this file except in compliance 
 * with the License. See the LICENSE.txt file distributed with this work 
 * for the full License.
 *
 * 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.
 */
//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] a = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int fromIndex = 2;
        int toIndex = 2;
        byte key = 2;
        System.out.println(find(a, fromIndex, toIndex, key));
    }

    /**
     * Retrieve a byte from a byte array.
     * 
     * @param a
     *        the byte array to look in
     * @param fromIndex
     *        the position from which to start looking
     * @param toIndex
     *        the position up to which to look
     * @param key
     *        the byte to find
     * 
     * @return the position of the byte in the array, or -1 if the byte was not
     *         found in the array
     * 
     */
    public static int find(byte[] a, int fromIndex, int toIndex, byte key) {
        int result = -1;

        if (fromIndex < 0) {
            fromIndex = 0;
        }
        toIndex = Math.min(toIndex, a.length);

        for (int i = fromIndex; fromIndex < toIndex && result == -1
                && i < toIndex; i++) {
            if (a[i] == key) {
                result = i;
            }
        }

        return result;
    }

    /**
     * Look for a sequence of bytes in a byte array.
     * 
     * @param a
     *        the byte array to look in
     * @param fromIndex
     *        the position from which to start looking
     * @param toIndex
     *        the position up to which to look
     * @param key
     *        the bytes to find
     * 
     * @return the position of the bytes in the array, or -1 if the bytes were
     *         not found in the array
     */
    public static int find(byte[] a, int fromIndex, int toIndex, byte[] key) {
        int result = -1;

        int sublen = key.length;
        int maxpos, first, sp = 0;

        maxpos = Math.min(toIndex, a.length) - sublen;

        for (first = fromIndex; sp != sublen && first <= maxpos; first++) {
            first = find(a, first, maxpos, key[0]);

            if ((first < 0) || (first > maxpos)) {
                break;
            }

            for (sp = 1; sp < sublen; sp++) {
                if (a[first + sp] != key[sp]) {
                    sp = sublen;
                }
            }
        }

        if (sublen == 0) {
            result = 0;
        } else if (sp == sublen) {
            result = (first - 1);
        }

        return result;
    }
}

Related Tutorials