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

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

Description

Determines whether the specified byte array starts with the specific bytes.

License

Open Source License

Parameter

Parameter Description
bytes The array whose start is tested.
startBytes The byte array whose presence at the start of the array is tested.

Return

'true' when the array starts with the specified start bytes, 'false' otherwise.

Declaration

private static boolean startsWith(byte[] array, byte[] startBytes) 

Method Source Code

//package com.java2s;
/*//from   w w w  .j a  v a  2s .c  o m
 * Copyright (c) 2006 - 2008 Aduna.
 * All rights reserved.
 *
 * Licensed under the Open Software License version 3.0.
 */

public class Main {
    /**
     * Determines whether the specified byte array starts with the specific bytes.
     *
     * @param bytes The array whose start is tested.
     * @param startBytes The byte array whose presence at the start of the array is tested.
     * @return 'true' when the array starts with the specified start bytes, 'false' otherwise.
     */
    private static boolean startsWith(byte[] array, byte[] startBytes) {
        if (array == null || startBytes == null || array.length < startBytes.length) {
            return false;
        }

        for (int i = 0; i < startBytes.length; i++) {
            if (array[i] != startBytes[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[] prefix)
  4. startsWith(byte[] bytes, int offset, byte... prefix)
  5. startsWith(byte[] bytes, String str, int offset)
  6. startsWith(byte[] bytes, String text)
  7. startsWith(byte[] checkMe, byte[] maybePrefix)