Java Array Equal arrayEquals(byte[] left, byte[] right)

Here you can find the source of arrayEquals(byte[] left, byte[] right)

Description

byte array compare

License

Open Source License

Parameter

Parameter Description
left a parameter
right a parameter

Return

true if equal

Declaration

public static boolean arrayEquals(byte[] left, byte[] right) 

Method Source Code

//package com.java2s;
/*/*from w w w  .  j  ava 2 s  . c  o m*/
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2008-2012 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library 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
 * Lesser General Public License for more details. You should have received
 * a copy of the GNU Lesser General Public License along with this library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */

public class Main {
    /**
     * byte array compare
     * @param left
     * @param right
     * @return true if equal
     */
    public static boolean arrayEquals(byte[] left, byte[] right) {
        if (left == null) {
            return ((right == null) ? true : false);
        }
        if (right == null) {
            return ((left == null) ? true : false);
        }
        if (left.length != right.length)
            return false;
        for (int i = 0; i < left.length; i++) {
            if (left[i] != right[i])
                return false;
        }
        return true;
    }

    /**
     * byte array compare
     * @param left
     * @param right
     * @param length
     * @return true if equal
     */
    public static boolean arrayEquals(byte[] left, byte[] right, int length) {
        if (left == null) {
            return ((right == null) ? true : false);
        }
        if (right == null) {
            return ((left == null) ? true : false);
        }

        // If one of left or right is shorter than length, arrays
        // must be same length to be equal.
        if (left.length < length || right.length < length)
            if (left.length != right.length)
                return false;

        int minarray = (left.length < right.length) ? left.length : right.length;
        int minlen = (length < minarray) ? length : minarray;

        for (int i = 0; i < minlen; i++) {
            if (left[i] != right[i])
                return false;
        }
        return true;
    }
}

Related

  1. allEquals(int y, int y2, int y3)
  2. areEqualArrays(Object[] a1, Object[] a2)
  3. arrayEq(byte[] a, byte[] b)
  4. arrayEqual(char[] a, char[] b, int aStart, int bStart, int length)
  5. arrayEquals(byte[] data, byte[] expected)
  6. arrayEquals(int[] a1, int[] a2)
  7. arrayEquals(int[] arr1, int[] arr2)
  8. arrayEquals(Object[] a, Object[] b)
  9. arrayEquals(Object[] a, Object[] b)