is ByteBuffer Predecessor Equals - Java java.nio

Java examples for java.nio:ByteBuffer

Description

is ByteBuffer Predecessor Equals

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public static final boolean isPredecessorEquals(final ByteBuffer bb1,
            final ByteBuffer bb2) {
        final int l1 = bb1.limit();
        final int l2 = bb2.limit();
        return isPredecessorEquals(bb1, 0, l1, bb2, 0, l2);
    }//from   www.  ja  v a 2 s.  c  om

    public static final boolean isPredecessorEquals(final ByteBuffer bb1,
            final int offset1, final int l1, final ByteBuffer bb2,
            final int offset2, final int l2) {
        // return v1.compareToIgnoreCase(v2) <= 0;
        if (l1 <= 0) {
            return false;
        } else if (l2 <= 0) {
            return true;
        }
        return compareTo(bb1.array(), offset1, l1, bb2.array(), offset2, l2) <= 0;
    }

    public static final int compareTo(ByteBuffer bb1, ByteBuffer bb2) {
        return compareTo(bb1.array(), 0, bb1.limit(), bb2.array(), 0,
                bb2.limit());
    }

    /**
     *
     * @param bs1
     * @param offset1
     * @param len1
     *            relative length
     * @param bs2
     * @param offset2
     * @param len2
     *            relative length
     * @return
     */
    public static final int compareTo(byte[] bs1, int offset1, int len1,
            byte[] bs2, int offset2, int len2) {
        final int n = offset1 + Math.min(len1, len2);
        while (offset1 < n) {
            byte c1 = bs1[offset1++];
            byte c2 = bs2[offset2++];
            if (c1 != c2) {
                return c1 - c2;
            }
        }
        return len1 - len2;
    }
}

Related Tutorials