compare ByteBuffer To another ByteBuffer - Java java.nio

Java examples for java.nio:ByteBuffer

Description

compare ByteBuffer To another ByteBuffer

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    public static final int compareTo(ByteBuffer bb1, ByteBuffer bb2) {
        return compareTo(bb1.array(), 0, bb1.limit(), bb2.array(), 0,
                bb2.limit());/*from w w w .  j  a v  a2 s .com*/
    }

    /**
     *
     * @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