Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2015 Eclipse RDF4J contributors, Aduna, and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Distribution License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *******************************************************************************/

public class Main {
    /**
     * Retrieve a byte from a byte array.
     * 
     * @param a
     *        the byte array to look in
     * @param fromIndex
     *        the position from which to start looking
     * @param toIndex
     *        the position up to which to look
     * @param key
     *        the byte to find
     * @return the position of the byte in the array, or -1 if the byte was not found in the array
     */
    public static int find(byte[] a, int fromIndex, int toIndex, byte key) {
        int result = -1;

        if (fromIndex < 0) {
            fromIndex = 0;
        }
        toIndex = Math.min(toIndex, a.length);

        for (int i = fromIndex; fromIndex < toIndex && result == -1 && i < toIndex; i++) {
            if (a[i] == key) {
                result = i;
            }
        }

        return result;
    }

    /**
     * Look for a sequence of bytes in a byte array.
     * 
     * @param a
     *        the byte array to look in
     * @param fromIndex
     *        the position from which to start looking
     * @param toIndex
     *        the position up to which to look
     * @param key
     *        the bytes to find
     * @return the position of the bytes in the array, or -1 if the bytes were not found in the array
     */
    public static int find(byte[] a, int fromIndex, int toIndex, byte[] key) {
        int result = -1;

        int sublen = key.length;
        int maxpos, first, sp = 0;

        maxpos = Math.min(toIndex, a.length) - sublen;

        for (first = fromIndex; sp != sublen && first <= maxpos; first++) {
            first = find(a, first, maxpos, key[0]);

            if ((first < 0) || (first > maxpos)) {
                break;
            }

            for (sp = 1; sp < sublen; sp++) {
                if (a[first + sp] != key[sp]) {
                    sp = sublen;
                }
            }
        }

        if (sublen == 0) {
            result = 0;
        } else if (sp == sublen) {
            result = (first - 1);
        }

        return result;
    }
}