Android Byte Array Compare compareNotNull(byte[] data1, byte[] data2)

Here you can find the source of compareNotNull(byte[] data1, byte[] data2)

Description

Compare the contents of two byte arrays.

License

Open Source License

Parameter

Parameter Description
data1 the first byte array (must not be null)
data2 the second byte array (must not be null)

Return

the result of the comparison (-1, 1 or 0)

Declaration

public static int compareNotNull(byte[] data1, byte[] data2) 

Method Source Code

//package com.java2s;
/*//from  w  w  w.  j a va  2  s  .c o  m
 * Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group
 */

public class Main {
    /**
     * Compare the contents of two byte arrays. If the content or length of the first array is smaller than the second array, -1 is returned. If the content or length of the second array is smaller than the first array, 1 is returned. If the contents and lengths are the same, 0 is returned.
     * 
     * @param data1
     *          the first byte array (must not be null)
     * @param data2
     *          the second byte array (must not be null)
     * @return the result of the comparison (-1, 1 or 0)
     */
    public static int compareNotNull(byte[] data1, byte[] data2) {
        int len = Math.min(data1.length, data2.length);
        for (int i = 0; i < len; i++) {
            byte b = data1[i];
            byte b2 = data2[i];
            if (b != b2) {
                return b > b2 ? 1 : -1;
            }
        }
        int c = data1.length - data2.length;
        return c == 0 ? 0 : (c < 0 ? -1 : 1);
    }
}

Related

  1. compare(byte[] a, int a_pos, byte[] b, int b_pos, int length)
  2. compare(byte[] as, byte[] bs)
  3. compareByteArray(byte[] src, byte[] dst)
  4. compareByteArray(byte[] src, int srcOffset, byte[] dst, int dstOffset, int length)
  5. compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)
  6. compareSecure(byte[] test, byte[] good)
  7. compareTo(byte[] buffer1, int offset1, int length1, byte[] buffer2, int offset2, int length2)
  8. compareTo(final byte[] left, final byte[] right)
  9. areEqual(byte[] a, byte[] b)