Android Byte Array Compare compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)

Here you can find the source of compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)

Description

Compares two byte array lexicographically..

Parameter

Parameter Description
src the byte array
srcOffset the start position of the first byte array
dst the byte array
dstOffset the start position of the second byte array

Return

the value 0 if the argument string is equal to this string; a value less than 0 if this string is lexicographically less than the string argument; and a value greater than 0 if this string is lexicographically greater than the string argument.

Declaration

public static int compareByteArray(byte[] src, int srcOffset,
        int srcLen, byte[] dst, int dstOffset, int dstLen) 

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);
    }/*from  w w w  . ja  va2  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. compare(byte[] a, byte[] b)
  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)
  6. compareNotNull(byte[] data1, byte[] data2)
  7. compareSecure(byte[] test, byte[] good)
  8. compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
  9. compareTo(final byte[] left, final byte[] right)