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 boolean startsWith(byte[] array, byte[] prefix) {
        final int l1 = array.length;
        final int l2 = prefix.length;
        return startsWith(array, 0, l1, prefix, 0, l2);
    }

    /**
     * 
     * @param array
     * @param limit1
     * @param prefix
     * @param limit2
     * @return
     */
    public static final boolean startsWith(final byte[] array, final int offset1, final int limit1,
            final byte[] prefix, final int offset2, final int limit2) {
        final int len1 = limit1 - offset1;
        final int len2 = limit2 - offset2;
        if (len1 >= len2) {
            for (int i = 0; i < len2; i++) {
                if (array[offset1 + i] != prefix[offset2 + i]) {
                    return false;
                }
            }
            return true;
        }
        return false;
    }

    public static final boolean startsWith(final ByteBuffer bb, final byte[] prefix) {
        return startsWith(bb.array(), 0, bb.limit(), prefix, 0, prefix.length);
    }
}