Return first position of symbol in byte array - Java java.lang

Java examples for java.lang:byte Array

Description

Return first position of symbol in byte array

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        byte[] btBuff = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        int iOffset = 2;
        byte[] btSearch = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(indexOf(btBuff, iOffset, btSearch));
    }//from  w ww.jav a  2s.co  m

    /**
     * Return first position of symbol in byte array
     * @param btBuff buffer to search
     * @param iOffset start offset
     * @param btSearch symbol to search
     * @return first position of symbol in buffer, -1 if not found
     * @author Thai Hoang Hiep
     */
    ////////////////////////////////////////////////////////
    public static int indexOf(byte[] btBuff, int iOffset, byte[] btSearch) {
        int iLength = btBuff.length - btSearch.length + 1;
        for (int iIndex = iOffset; iIndex < iLength; iIndex++) {
            if (btSearch[0] == btBuff[iIndex]) {
                int i = 1;
                while (i < btSearch.length) {
                    if (btSearch[i] != btBuff[iIndex + i])
                        break;
                    i++;
                }
                if (i >= btSearch.length)
                    return iIndex;
            }
        }
        return -1;
    }

    /**
     * Return first position of symbol in byte array
     * @param btBuff buffer to search
     * @param iOffset start offset
     * @param btSearch symbol to search
     * @return first position of symbol in buffer, -1 if not found
     * @author Thai Hoang Hiep
     */
    ////////////////////////////////////////////////////////
    public static int indexOf(byte[] btBuff, int iOffset, byte btSearch) {
        int iLength = btBuff.length;
        for (int iIndex = iOffset; iIndex < iLength; iIndex++) {
            if (btBuff[iIndex] == btSearch)
                return iIndex;
        }
        return -1;
    }

    /**
     * Return first position of symbol in byte array
     * @param btBuff buffer to search
     * @param iOffset start offset
     * @param btSearch symbol to search
     * @param btEscape symbol to escape
     * @return first position of symbol in buffer, -1 if not found
     * @author Thai Hoang Hiep
     */
    ////////////////////////////////////////////////////////
    public static int indexOf(byte[] btBuff, int iOffset, byte btSearch,
            byte btEscape) {
        int iLength = btBuff.length;
        int iEscapeIndex = -1;
        for (int iIndex = iOffset; iIndex < iLength; iIndex++) {
            if (btBuff[iIndex] == btEscape) {
                if (iEscapeIndex < 0)
                    iEscapeIndex = iIndex;
            } else if (btBuff[iIndex] == btSearch) {
                if (iEscapeIndex < 0 || (iIndex - iEscapeIndex) % 2 == 0)
                    return iIndex;
            } else if (iEscapeIndex >= 0)
                iEscapeIndex = -1;
        }
        return -1;
    }
}

Related Tutorials