Android Byte Array Equal equalByteArray(byte[] src, byte[] dst)

Here you can find the source of equalByteArray(byte[] src, byte[] dst)

Description

Compares this byte arrary to the specified object.

Parameter

Parameter Description
src the first byte array
tag the second byte array against.

Return

true if the String are equal; false otherwise.

Declaration

public static boolean equalByteArray(byte[] src, byte[] dst) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w  w  w .ja  v  a2 s .  c  om
     * Compares this byte arrary to the specified object.
     * The result is <code>true</code> if and only if the argument is not
     * <code>null</code> and is a <code>String</code> object that represents
     * the same sequence of characters as this object.
     *
     * @param src
     *            the first byte array
     * @param tag
     *            the second byte array
     *                     against.
     * @return  <code>true</code> if the <code>String </code>are equal;
     *          <code>false</code> otherwise.
     */
    public static boolean equalByteArray(byte[] src, byte[] dst) {
        return equalByteArray(src, 0, src.length, dst, 0, dst.length);
    }

    public static boolean equalByteArray(byte[] src, int srcOffset,
            int srcLen, byte[] dst, int dstOffset, int dstLen) {
        if (compareByteArray(src, srcOffset, srcLen, dst, dstOffset, dstLen) == 0) {
            return true;
        } else {
            return false;
        }
    }

    public static int compareByteArray(byte[] src, byte[] dst) {
        return compareByteArray(src, 0, src.length, dst, 0, dst.length);
    }

    public static int compareByteArray(byte[] src, int srcOffset,
            byte[] dst, int dstOffset, int length) {
        return compareByteArray(src, srcOffset, length, dst, dstOffset,
                length);
    }

    /**
     * Compares two byte array lexicographically..
     * 
     * @param src
     *            the byte array
     * @param srcOffset
     *            the start position of the first byte array
     * @param dst
     *            the byte array
     * @param dstOffset
     *            the start position of the second byte array
     * @return  the value <code>0</code> if the argument string is equal to
     *          this string; a value less than <code>0</code> if this string
     *          is lexicographically less than the string argument; and a
     *          value greater than <code>0</code> if this string is
     *          lexicographically greater than the string argument.
     */
    public static int compareByteArray(byte[] src, int srcOffset,
            int srcLen, byte[] dst, int dstOffset, int dstLen) {
        char c1, c2;
        if (src == null || srcOffset < 0 || srcLen < 0) {
            return Integer.MIN_VALUE;
        }
        if (dst == null || dstOffset < 0 || dstLen < 0) {
            return Integer.MIN_VALUE;
        }
        int n = Math.min(srcLen, dstLen);
        if ((srcOffset + n) > src.length || (dstOffset + n) > dst.length) {
            return Integer.MIN_VALUE;
        }
        for (int i = 0; i < n; i++) {
            // compare the byte
            c1 = (char) (src[srcOffset + i] & 0xFF);
            c2 = (char) (dst[dstOffset + i] & 0xFF);
            if (c1 != c2) {
                return c1 - c2;
            }
        }
        return srcLen - dstLen;
    }
}

Related

  1. equals(byte[] array1, byte[] array2, int length)
  2. equals(byte[] array1, byte[] array2, int length)
  3. equals(final byte[] pArrayA, final int pOffsetA, final byte[] pArrayB, final int pOffsetB, final int pLength)
  4. equals(final byte[] pArrayA, final int pOffsetA, final byte[] pArrayB, final int pOffsetB, final int pLength)
  5. isEqual(byte[] first, byte[] second)
  6. equalByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)
  7. equals(byte[] a1, byte[] a2)
  8. equals(byte[] array1, byte[] array2)
  9. equals(byte[] left, byte[] right)