is ByteBuffer Successor - Java java.nio

Java examples for java.nio:ByteBuffer

Description

is ByteBuffer Successor

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**/*from  w  w  w  . jav a  2  s  . c o m*/
     *
     * @param bb1
     * @param bb2
     * @return
     */
    public static final boolean isSuccessor(final ByteBuffer bb1,
            final ByteBuffer bb2) {
        final int l1 = bb1.limit();
        final int l2 = bb2.limit();
        return isSuccessor(bb1, 0, l1, bb2, 0, l2);
    }

    /**
     *
     * @param bb1
     * @param offset1
     * @param l1
     *            relative length
     * @param bb2
     * @param offset2
     * @param l2
     *            relative length
     * @return
     */
    public static final boolean isSuccessor(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 true;
        } else if (l2 <= 0) {
            return false;
        }
        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