Android Byte Array Compare compare(byte[] a, byte[] b)

Here you can find the source of compare(byte[] a, byte[] b)

Description

Compare tow bytearrays.

License

Open Source License

Parameter

Parameter Description
a a parameter
b a parameter

Return

true if contents of two byte array are match.

Declaration

public static boolean compare(byte[] a, byte[] b) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from   w  w w  .  j  av a2s .c  o m*/
     * Compare tow bytearrays.
     * 
     * @param a
     * @param b
     * @return true if contents of two byte array are match.
     */
    public static boolean compare(byte[] a, byte[] b) {
        if (a == null || b == null) {
            return a == b;
        }
        if (a.length != b.length) {
            return false;
        }
        for (int i = 0; i < a.length; i++) {
            if (a[i] != b[i]) {
                return false;
            }
        }
        return true;
    }

    /**
     * Compare parts of bytearrays.
     * 
     * @param a
     * @param a_pos
     * @param b
     * @param b_pos
     * @param length
     * @return true if match.
     */
    public static boolean compare(byte[] a, int a_pos, byte[] b, int b_pos,
            int length) {
        if (a == null || b == null) {
            return length == 0;
        }
        if ((a.length < a_pos + length) || (b.length < b_pos + length)) {
            return false;
        }
        for (int i = 0; i < length; i++) {
            if (a[a_pos + i] != b[b_pos + i]) {
                return false;
            }
        }
        return true;
    }
}

Related

  1. findWhereDiffer(byte[] bytes1, byte[] bytes2, int limit)
  2. compare(byte[] a, int a_pos, byte[] b, int b_pos, int length)
  3. compare(byte[] as, byte[] bs)
  4. compareByteArray(byte[] src, byte[] dst)
  5. compareByteArray(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)