match Up to One Of Or EOS in CharBuffer - Java java.nio

Java examples for java.nio:CharBuffer

Description

match Up to One Of Or EOS in CharBuffer

Demo Code


import java.nio.CharBuffer;

public class Main{
    public static int matchUptoOneOfOrEOS(CharBuffer buf,
            char[] candidateCharacters, int from, int limit, boolean isEOS) {
        for (int i = from; i < limit; i++) {
            if (CharBufferUtils.isOneOf(i, buf, candidateCharacters)) {
                return i;
            }/*w w  w.  ja  va2  s  . c o m*/
        }

        if (isEOS) {
            return limit;
        }

        return -1;
    }
    /**
     * Is the character at index 'pos' of 'buf' one of the characters in the
     * 'candidateCharacters' array?
     */
    public static boolean isOneOf(int pos, CharBuffer buf,
            char[] candidateCharacters) {
        char c = buf.get(pos);

        for (int i = 0; i < candidateCharacters.length; i++) {
            if (candidateCharacters[i] == c) {
                return true;
            }
        }

        return false;
    }
}

Related Tutorials