Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*  Copyright (c) 2010 Xiaoyun Zhu
 * 
 *  Permission is hereby granted, free of charge, to any person obtaining a copy  
 *  of this software and associated documentation files (the "Software"), to deal  
 *  in the Software without restriction, including without limitation the rights  
 *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell  
 *  copies of the Software, and to permit persons to whom the Software is  
 *  furnished to do so, subject to the following conditions:
 *  
 *  The above copyright notice and this permission notice shall be included in  
 *  all copies or substantial portions of the Software.
 *  
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR  
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  
 *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE  
 *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER  
 *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,  
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN  
 *  THE SOFTWARE.  
 */

import java.nio.ByteBuffer;

public class Main {
    public static final int compareToP(ByteBuffer bb1, ByteBuffer bb2) {
        final int offset1 = bb1.position();
        final int offset2 = bb2.position();
        final byte[] array1 = bb1.array();
        final byte[] array2 = bb2.array();
        final int len1 = bb1.remaining();
        final int len2 = bb2.remaining();
        return compareTo(array1, offset1, len1, array2, offset2, len2);
    }

    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;
    }
}