Here you can find the source of compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)
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 |
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.
public static int compareByteArray(byte[] src, int srcOffset, int srcLen, byte[] dst, int dstOffset, int dstLen)
//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; } }