/** * Compares two {@link java.nio.ByteBuffer}s and checks whether the first ByteBuffer is smaller than or equal to the second. * * @param a First ByteBuffer * @param b Second ByteBuffer * @return true if the first ByteBuffer is smaller than or equal to the second */ public static boolean isSmallerOrEqualThan(ByteBuffer a, ByteBuffer b) { return compare(a, b)<=0; }
/** * Compares two {@link java.nio.ByteBuffer}s and checks whether the first ByteBuffer is smaller than the second. * * @param a First ByteBuffer * @param b Second ByteBuffer * @return true if the first ByteBuffer is smaller than the second */ public static boolean isSmallerThan(ByteBuffer a, ByteBuffer b) { return compare(a, b)<0; }
/** * Compares two {@link java.nio.ByteBuffer}s according to their byte order (and not the byte value). * <p> * * @param b1 First ByteBuffer * @param b2 Second ByteBuffer * @return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second. */ public static int compare(ByteBuffer b1, ByteBuffer b2) { if (b1 == b2) { return 0; } int p1=b1.position(), p2 = b2.position(); while (p1<b1.limit() || p2<b2.limit()) { if (p1>=b1.limit()) return -1; else if (p2>=b2.limit()) return 1; else { int cmp = compare(b1.get(p1), b2.get(p2)); if (cmp!=0) return cmp; } p1++; p2++; } return 0; //Must be equal }