index Of byte in byte array by offset and limit - Java java.lang

Java examples for java.lang:byte Array

Description

index Of byte in byte array by offset and limit

Demo Code


//package com.java2s;

import java.nio.ByteBuffer;

public class Main {
    /**/*from   ww w .  ja  v a2 s .c o m*/
     *
     * @param bb
     * @param offset
     *            absolute
     * @param limit
     *            absolute
     * @param b
     * @return absolute index
     */
    public static final int indexOf(final byte[] bb, final int offset,
            final int limit, final byte b) {
        for (int i = offset; i < limit; i++) {
            if (bb[i] == b) {
                return i;
            }
        }
        return -1;
    }

    /**
     *
     * @param text
     * @param offset
     * @param len
     *            relative
     * @param s
     * @return absolute index
     */
    public final static int indexOf(final byte[] text, final int offset,
            final int len, final byte[] s) {
        return indexOf(text, offset, len, s, 0, s.length);
    }

    /**
     *
     * @param text
     * @param offset
     * @param len1
     *            relative
     * @param s
     * @param offset2
     * @param len2
     *            relative
     * @return absolute index
     */
    public final static int indexOf(final byte[] text, final int offset,
            final int len1, final byte[] s, final int offset2,
            final int len2) {
        if (len1 >= len2) {
            final int limit = offset + len1;
            int idx = 0;
            byte b;
            for (int i = offset; i < limit; i++) {
                b = text[i];
                if (b == s[offset2 + idx]) {
                    if (++idx == len2) {
                        return i - len2 + 1;
                    }
                } else {
                    idx = 0;
                }
            }
        }
        return -1;
    }

    public static final int indexOf(final ByteBuffer bb, final byte b) {
        return indexOf(bb.array(), 0, bb.limit(), b);
    }

    public final static int indexOf(final char[][] pairs, final char c) {
        int i = 0;
        for (final char[] pair : pairs) {
            if (c == pair[0]) {
                return i;
            }
            i++;
        }
        return -1;
    }

    public static final int indexOf(final byte[] array, final int position,
            final int limit, final byte[][] textsLower,
            final byte[][] textsUpper) {
        byte b;
        int[] idx = new int[textsLower.length];
        for (int i = position; i < limit; i++) {
            b = array[i];
            for (int j = 0; j < idx.length; j++) {
                final int textIdx = idx[j];
                final byte[] textLower = textsLower[j];
                final byte[] textUpper = textsUpper[j];
                if (b == textLower[textIdx] || b == textUpper[textIdx]) {
                    if (textIdx + 1 < textLower.length) {
                        idx[j] = textIdx + 1;
                    } else {
                        return i - textLower.length + 1;
                    }
                } else {
                    idx[j] = 0;
                }
            }
        }
        return -1;
    }
}

Related Tutorials