Android Byte Array Compare compareByteArray(byte[] src, byte[] dst)

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

Description

compare Byte Array

Declaration

public static int compareByteArray(byte[] src, byte[] dst) 

Method Source Code

//package com.java2s;

public class Main {
    public static int compareByteArray(byte[] src, byte[] dst) {
        return compareByteArray(src, 0, src.length, dst, 0, dst.length);
    }// w  w w .  j  a v a2 s.co  m

    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. findWhereDiffer(byte[] bytes1, byte[] bytes2, int limit)
  2. compare(byte[] a, byte[] b)
  3. compare(byte[] a, int a_pos, byte[] b, int b_pos, int length)
  4. compare(byte[] as, byte[] bs)
  5. compareByteArray(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)
  6. compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)
  7. compareNotNull(byte[] data1, byte[] data2)
  8. compareSecure(byte[] test, byte[] good)