Java Byte Array Equal bytesEqual(byte[] a1, byte[] a2)

Here you can find the source of bytesEqual(byte[] a1, byte[] a2)

Description

Test two byte arrays for (deep) equality.

License

Apache License

Parameter

Parameter Description
a1 Array 1
a2 Array 2

Return

True iff a1.length == a2.length and a1[i] == a2[i] for 0 <= i < a1.length

Declaration

public static boolean bytesEqual(byte[] a1, byte[] a2) 

Method Source Code

//package com.java2s;
/*// w  w w .j av  a  2  s  . c o m
 * Copyright 2008-2013 LinkedIn, Inc
 * 
 * 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 {
    /**
     * Test two byte arrays for (deep) equality. I think this exists in java 6
     * but not java 5
     * 
     * @param a1 Array 1
     * @param a2 Array 2
     * @return True iff a1.length == a2.length and a1[i] == a2[i] for 0 <= i <
     *         a1.length
     */
    public static boolean bytesEqual(byte[] a1, byte[] a2) {
        if (a1 == a2) {
            return true;
        } else if (a1 == null || a2 == null) {
            return false;
        } else if (a1.length != a2.length) {
            return false;
        } else {
            for (int i = 0; i < a1.length; i++)
                if (a1[i] != a2[i])
                    return false;
        }

        return true;
    }
}

Related

  1. byteArraysEqual(byte[] b1, byte[] b2)
  2. bytesEqual(byte[] a, byte[] b)
  3. bytesEqual(byte[] a, byte[] b, int len)
  4. bytesEqual(byte[] a, int aFrom, int aLen, byte[] b, int bFrom, int bLen)
  5. bytesEqual(byte[] a, int aOff, byte[] b, int bOff, int len)
  6. bytesEqual(byte[] a1, byte[] a2)
  7. bytesEqual(byte[] buf1, byte[] buf2, int off, int len)
  8. bytesEqual(final byte[] a, final byte[] b)
  9. bytesEqual(final byte[] b1, final int j, final byte[] b2, final int k, final int len)