Java Byte Array Equal byteArrayEquals(final byte[] lhs, final byte[] rhs)

Here you can find the source of byteArrayEquals(final byte[] lhs, final byte[] rhs)

Description

check that two byte arrays are equal.

License

Apache License

Parameter

Parameter Description
lhs a byte array
rhs another byte array.

Return

true if they are both equal (or both null)

Declaration

public static boolean byteArrayEquals(final byte[] lhs, final byte[] rhs) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /** check that two byte arrays are equal.  They may be <code>null</code>.
     *// ww w .jav a  2 s  .co  m
     * @param lhs a byte array
     * @param rhs another byte array.
     * @return <code>true</code> if they are both equal (or both
     * <code>null</code>)
     */
    public static boolean byteArrayEquals(final byte[] lhs, final byte[] rhs) {
        if (lhs == null && rhs != null || lhs != null && rhs == null) {
            return false;
        }
        if (lhs == rhs) {
            return true;
        }
        if (lhs.length != rhs.length) {
            return false;
        }
        for (int i = 0; i < lhs.length; i++) {
            if (lhs[i] != rhs[i]) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. byteArrayEquals(byte[] a1, byte[] a2)
  2. byteArrayEquals(byte[] b1, byte[] b2)
  3. byteArrayEquals(final byte[] actual, final byte[] expected)
  4. byteArraysEqual(byte[] b1, byte[] b2)
  5. bytesEqual(byte[] a, byte[] b)
  6. bytesEqual(byte[] a, byte[] b, int len)
  7. bytesEqual(byte[] a, int aFrom, int aLen, byte[] b, int bFrom, int bLen)