Java Array Starts With startsWith(byte[] array, byte[] prefix)

Here you can find the source of startsWith(byte[] array, byte[] prefix)

Description

Utility method to check if one byte array starts with a specified sequence of bytes.

License

Open Source License

Parameter

Parameter Description
array The array to check
prefix The prefix bytes to test for

Return

true if the array starts with the bytes from the prefix

Declaration

public static boolean startsWith(byte[] array, byte[] prefix) 

Method Source Code

//package com.java2s;
/**//from  ww  w . j  a v a2s .  com
 * Geotag
 * Copyright (C) 2007-2016 Andreas Schneider
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    /**
     * Utility method to check if one byte array starts with a specified sequence
     * of bytes.
     * 
     * @param array
     *          The array to check
     * @param prefix
     *          The prefix bytes to test for
     * @return true if the array starts with the bytes from the prefix
     */
    public static boolean startsWith(byte[] array, byte[] prefix) {
        if (array == prefix) {
            return true;
        }
        if (array == null || prefix == null) {
            return false;
        }
        int prefixLength = prefix.length;

        if (prefix.length > array.length) {
            return false;
        }

        for (int i = 0; i < prefixLength; i++) {
            if (array[i] != prefix[i]) {
                return false;
            }
        }

        return true;
    }
}

Related

  1. startsWith(byte a[], int from, byte b[])
  2. startsWith(byte[] arr, int offset, int len, byte[] pattern)
  3. startsWith(byte[] array, byte[] startBytes)
  4. startsWith(byte[] bytes, int offset, byte... prefix)
  5. startsWith(byte[] bytes, String str, int offset)
  6. startsWith(byte[] bytes, String text)