Java Array Start With arrayStartsWith(final byte[] array, final byte[] str)

Here you can find the source of arrayStartsWith(final byte[] array, final byte[] str)

Description

Check that a byte array starts with some byte values.

License

Apache License

Parameter

Parameter Description
array array to be checked, must not be null
str a byte string which will be checked as the start sequence of the array, must not be null

Exception

Parameter Description
NullPointerException if any argument is null

Return

true if the string is the start sequence of the array, false otherwise

Declaration

public static boolean arrayStartsWith(final byte[] array,
        final byte[] str) 

Method Source Code

//package com.java2s;
/* /*from   w ww  .j a  v  a  2s . c  o m*/
 * Copyright 2014 Igor Maznitsa (http://www.igormaznitsa.com).
 *
 * 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 {
    /**
     * Check that a byte array starts with some byte values.
     *
     * @param array array to be checked, must not be null
     * @param str a byte string which will be checked as the start sequence of the
     * array, must not be null
     * @return true if the string is the start sequence of the array, false
     * otherwise
     * @throws NullPointerException if any argument is null
     * @since 1.1
     */
    public static boolean arrayStartsWith(final byte[] array,
            final byte[] str) {
        boolean result = false;
        if (array.length >= str.length) {
            result = true;
            int index = str.length;
            while (--index >= 0) {
                if (array[index] != str[index]) {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }
}

Related

  1. arrayStartsWith(byte[] a, byte[] b)
  2. arrayStartsWith(byte[] array, byte[] target)
  3. arrayStartsWith(V[] pattern, V[] data)
  4. startWith(byte[] data, int start, byte[] find)
  5. startWith(byte[] startWith, byte[] bytes)
  6. startWith(char[] value, char c)