Java Code Examples for java.nio.CharBuffer#charAt()

The following examples show how to use java.nio.CharBuffer#charAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EditableSecureBuffer.java    From edslite with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Return the char at the specified offset within the buffer.
 */
@Override
public char charAt(int where) {
    if(VERBOSE_LOG) Logger.debug(TAG + ": in charAt");
    CharBuffer cb = _sb.getCharBuffer();
    if(cb == null)
        return ' ';
    cb.clear();
    int len = cb.capacity() - mGapLength;
    if (where < 0) {
        throw new IndexOutOfBoundsException("charAt: " + where + " < 0");
    } else if (where >= len) {
        throw new IndexOutOfBoundsException("charAt: " + where + " >= length " + len);
    }

    if (where >= mGapStart)
        return cb.charAt(where + mGapLength);
    else
        return cb.charAt(where);
}
 
Example 2
Source File: Converter.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private String[] parseLines(CharBuffer cb, Charset encoding) {
    List<String> lines = new java.util.ArrayList<String>();
    StringBuffer sb = new StringBuffer();
    while (cb.hasRemaining()) {
        while (cb.hasRemaining()) {
            char c = cb.get();
            if (c == '\n') {
                break;
            } else if (c == '\r') {
                if (cb.hasRemaining()) {
                    c = cb.charAt(0);
                    if (c == '\n')
                        cb.get();
                }
                break;
            } else {
                sb.append(c);
            }
        }
        lines.add(sb.toString());
        sb.setLength(0);
    }
    cb.rewind();
    return lines.toArray(new String[lines.size()]);
}
 
Example 3
Source File: TimedTextVerifier.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private String[] parseLines(CharBuffer cb, Charset encoding) {
    List<String> lines = new java.util.ArrayList<String>();
    StringBuffer sb = new StringBuffer();
    while (cb.hasRemaining()) {
        while (cb.hasRemaining()) {
            char c = cb.get();
            if (c == '\n') {
                break;
            } else if (c == '\r') {
                if (cb.hasRemaining()) {
                    c = cb.charAt(0);
                    if (c == '\n')
                        cb.get();
                }
                break;
            } else {
                sb.append(c);
            }
        }
        lines.add(sb.toString());
        sb.setLength(0);
    }
    cb.rewind();
    return lines.toArray(new String[lines.size()]);
}
 
Example 4
Source File: RestartOptions.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Token getWhitespace(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    StringBuffer sb = new StringBuffer();
    while (i < n) {
        char c = cb.charAt(i);
        if (Character.isWhitespace(c)) {
            sb.append(c);
            ++i;
        } else
            break;
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.SPACE, sb.toString());
    } else
        return null;
}
 
Example 5
Source File: OsglConfig.java    From java-tool with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test(Readable readable) {
    CharBuffer buf = CharBuffer.allocate(100);
    try {
        int n = readable.read(buf);
        if (n < 0) {
            return false;
        }
        buf.flip();
        for (int i = 0; i < n; ++i) {
            char c = buf.charAt(i);
            if (Character.isISOControl(c)) {
                if (c != '\n' && c != '\r') {
                    return true;
                }
            }
        }
        return false;
    } catch (IOException e) {
        throw E.ioException(e);
    } finally {
        buf.clear();
    }
}
 
Example 6
Source File: RestartOptions.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Token getIdent(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    char c;
    StringBuffer sb = new StringBuffer();
    c = (i < n) ? cb.charAt(i) : 0;
    if (XML.isNCNameCharStart(c)) {
        sb.append(c);
        ++i;
        while (i < n) {
            c = (i < n) ? cb.charAt(i) : 0;
            if (XML.isNCNameCharPart(c)) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.IDENT, sb.toString());
    } else
        return null;
}
 
Example 7
Source File: Condition.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Token getWhitespace(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    StringBuffer sb = new StringBuffer();
    while (i < n) {
        char c = cb.charAt(i);
        if (Character.isWhitespace(c)) {
            sb.append(c);
            ++i;
        } else
            break;
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.SPACE, sb.toString());
    } else
        return null;
}
 
Example 8
Source File: Condition.java    From ttt with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private static Token getIdent(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    char c;
    StringBuffer sb = new StringBuffer();
    c = (i < n) ? cb.charAt(i) : 0;
    if (XML.isNCNameCharStart(c)) {
        sb.append(c);
        ++i;
        while (i < n) {
            c = (i < n) ? cb.charAt(i) : 0;
            if (XML.isNCNameCharPart(c)) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.IDENT, sb.toString());
    } else
        return null;
}
 
Example 9
Source File: TestAll.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public int read(CharBuffer cb) throws IOException {
    if(cb==null) throw  new IOException("error");
    int i = 0;
    int count = 0;
    LOOP: while(cb.charAt(i)==' ') {
        i++;
        count++;
        if(count>KONST) break LOOP;            
    }
    return 0;
    
}
 
Example 10
Source File: GraphIndexTransaction.java    From hugegraph with Apache License 2.0 5 votes vote down vote up
private static String increaseString(String value) {
    int length = value.length();
    CharBuffer cbuf = CharBuffer.wrap(value.toCharArray());
    char last = cbuf.charAt(length - 1);
    E.checkArgument(last == '!' || LongEncoding.validB64Char(last),
                    "Invalid character '%s' for String index", last);
    cbuf.put(length - 1,  (char) (last + 1));
    return cbuf.toString();
}
 
Example 11
Source File: RestartOptions.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Token getString(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    char d;                         // string delimiter
    StringBuffer sb = new StringBuffer();
    // get string delimiter
    d = (i < n) ? cb.charAt(i) : 0;
    if ((d == '\'') || (d == '\"')) {
        ++i;
        // get delimited content, handling escapes
        char c = 0;
        while (i < n) {
            c = (i < n) ? cb.charAt(i) : 0;
            if (c == d) {
                ++i;
                break;
            } else if (c == '\\') {
                ++i;
                if (i < n) {
                    c = (i < n) ? cb.charAt(i) : 0;
                    sb.append(c);
                    ++i;
                } else
                    break;
            } else {
                sb.append(c);
                ++i;
            }
        }
        if (c != d)
            sb.setLength(0);
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.STRING, sb.toString());
    } else
        return null;
}
 
Example 12
Source File: Condition.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static Token getString(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int n = cb.remaining();         // # indices remaining
    char d;                         // string delimiter
    StringBuffer sb = new StringBuffer();
    // get string delimiter
    d = (i < n) ? cb.charAt(i) : 0;
    if ((d == '\'') || (d == '\"')) {
        ++i;
        // get delimited content, handling escapes
        char c = 0;
        while (i < n) {
            c = (i < n) ? cb.charAt(i) : 0;
            if (c == d) {
                ++i;
                break;
            } else if (c == '\\') {
                ++i;
                if (i < n) {
                    c = (i < n) ? cb.charAt(i) : 0;
                    sb.append(c);
                    ++i;
                } else
                    break;
            } else {
                sb.append(c);
                ++i;
            }
        }
        if (c != d)
            sb.setLength(0);
    }
    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.STRING, sb.toString());
    } else
        return null;
}
 
Example 13
Source File: GerritConnection.java    From gerrit-events with MIT License 5 votes vote down vote up
/**
 * Offers lines in buffer to queue.
 *
 * @param cb a buffer to have received text data.
 * @return the line string. null if no EOL, otherwise buffer is compacted.
 */
private String getLine(CharBuffer cb) {
    String line = null;
    int pos = cb.position();
    int limit = cb.limit();
    cb.flip();
    for (int i = 0; i < cb.length(); i++) {
        if (cb.charAt(i) == '\n') {
            line = getSubSequence(cb, 0, i).toString();
            cb.position(i + 1);
            break;
        }
    }
    if (line != null) {
        cb.compact();
        if (eventBuffer != null) {
            eventBuffer.append(line);
            String eventString = eventBuffer.toString();
            eventBuffer = null;
            line = eventString;
        }
        line.trim();
    } else {
        if (cb.length() > 0) {
            if (cb.length() == cb.capacity()) {
                if (eventBuffer == null) {
                    logger.debug("Encountered big event.");
                    eventBuffer = new StringBuilder();
                }
                eventBuffer.append(getSubSequence(cb, 0, pos));
            } else {
                cb.position(pos);
                cb.limit(limit);
            }
        } else {
            cb.clear();
        }
    }
    return line;
}
 
Example 14
Source File: StaxExtractingProcessor.java    From jesterj with Apache License 2.0 5 votes vote down vote up
private void decrementPath(CharBuffer path) {
  int lastSlash = 0;
  path.rewind();
  for (int i = 0; i < path.limit(); i++) {
    if (path.charAt(i) == '/') {
      lastSlash = i;
    }
  }
  path.limit(lastSlash);
}
 
Example 15
Source File: LoadTextUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
private static ConvertResult convertLineSeparatorsToSlashN(@Nonnull CharBuffer buffer) {
  int dst = 0;
  char prev = ' ';
  int crCount = 0;
  int lfCount = 0;
  int crlfCount = 0;

  final int length = buffer.length();
  final char[] bufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);

  for (int src = 0; src < length; src++) {
    char c = bufferArray != null ? bufferArray[src] : buffer.charAt(src);
    switch (c) {
      case '\r':
        if (bufferArray != null) bufferArray[dst++] = '\n';
        else buffer.put(dst++, '\n');
        crCount++;
        break;
      case '\n':
        if (prev == '\r') {
          crCount--;
          crlfCount++;
        }
        else {
          if (bufferArray != null) bufferArray[dst++] = '\n';
          else buffer.put(dst++, '\n');
          lfCount++;
        }
        break;
      default:
        if (bufferArray != null) bufferArray[dst++] = c;
        else buffer.put(dst++, c);
        break;
    }
    prev = c;
  }

  String detectedLineSeparator = guessLineSeparator(crCount, lfCount, crlfCount);

  CharSequence result = buffer.length() == dst ? buffer : buffer.subSequence(0, dst);
  return new ConvertResult(result, detectedLineSeparator);
}
 
Example 16
Source File: TableBlockParser.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private TableRowNode parseRow(String rowStr, InlineParser inlineParser) {
  rowStr = rowStr.trim();
  if (rowStr.startsWith("|")) {
    rowStr = rowStr.substring(1);
  }

  TableRowNode row = new TableRowNode();

  boolean isEscaped = false;
  int currentColumn = 0;
  CharBuffer data = CharBuffer.wrap(rowStr);
  for (int index = 0; data.hasRemaining(); index++) {
    if (isEscaped) {
      isEscaped = false;
      continue;
    }

    char c = data.charAt(index);
    if (c == '\\') {
      isEscaped = true;
      continue;
    }

    if (c == '|' || (index + 1) >= data.remaining()) {
      int end = c == '|' ? index : index + 1;
      String content = data.subSequence(0, end).toString();
      data.position(data.position() + end);

      int colSpan = 0;
      while (data.hasRemaining() && data.charAt(0) == '|') {
        colSpan++;
        data.position(data.position() + 1);
      }
      index = -1; // Account for post-forloop increment.
      Alignment alignment =
          currentColumn < columns.size() ? columns.get(currentColumn) : Alignment.NONE;
      currentColumn += colSpan;

      TableCellNode cell = new TableCellNode(colSpan, alignment);
      inlineParser.parse(content.trim(), cell);
      row.appendChild(cell);
    }
  }

  return row;
}
 
Example 17
Source File: Condition.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static Token getNumeric(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int j;                          // helper index
    int k;                          // helper index
    int n = cb.remaining();         // # indices remaining
    int m = 0;                      // # digits in mantissa
    int e = 0;                      // # digits in exponent
    char c;
    StringBuffer sb = new StringBuffer();

    // integral component
    j = i;
    c = (i < n) ? cb.charAt(i) : 0;
    if (c == '0') {
        sb.append(c);
        ++i;
    } else if ((c >= '1') && (c <= '9')) {
        sb.append(c);
        ++i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    m += j - i;

    // fractional component
    j = i;
    c = (i < n) ? cb.charAt(i) : 0;
    if (c == '.') {
        sb.append(c);
        ++i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    m += j - i;

    // ensure mantissa is non-empty
    if (m == 0)
        return null;

    // exponent component
    k = sb.length();
    c = (i < n) ? cb.charAt(i) : 0;
    if ((c == 'e') || (c == 'E')) {
        sb.append('E');
        ++i;
        c = (i < n) ? cb.charAt(i) : 0;
        if ((c == '+') || (c == '-')) {
            sb.append(c);
            ++i;
        }
        j = i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
        e += j - i;
    }
    if (e == 0)
        sb.setLength(k);

    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.NUMERIC, sb.toString());
    } else
        return null;
}
 
Example 18
Source File: Condition.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static Token getToken(CharBuffer cb, boolean consume, boolean skipWhitespace) {
    Token t;
    int   p0 = cb.position();
    int   n  = cb.remaining();
    char  c1 = (n > 0) ? cb.charAt(0) : 0;
    char  c2 = (n > 1) ? cb.charAt(1) : 0;
    // the following clauses are order dependent
    if (c1 == 0) {
        t = Token.EOS;
    } else if (c1 == '(') {
        t = Token.OPEN;
    } else if (c1 == ')') {
        t = Token.CLOSE;
    } else if (c1 == ',') {
        t = Token.COMMA;
    } else if (c1 == '+') {
        t = Token.PLUS;
    } else if (c1 == '-') {
        t = Token.MINUS;
    } else if (c1 == '*') {
        t = Token.MULTIPLY;
    } else if (c1 == '/') {
        t = Token.DIVIDE;
    } else if (c1 == '%') {
        t = Token.MODULO;
    } else if (c1 == '=') {
        if (c2 == '=') {
            t = Token.EQ;
        } else
            t = null;
    } else if (c1 == '!') {
        if (c2 == '=') {
            t = Token.NEQ;
        } else
            t = Token.NOT;
    } else if (c1 == '<') {
        if (c2 == '=')
            t = Token.LEQ;
        else
            t = Token.LT;
    } else if (c1 == '>') {
        if (c2 == '=')
            t = Token.GEQ;
        else
            t = Token.GT;
    } else if (c1 == '|') {
        if (c2 == '|')
            t = Token.OR;
        else
            t = null;
    } else if (c1 == '&') {
        if (c2 == '&')
            t = Token.AND;
        else
            t = null;
    } else if (isString(cb)) {
        t = getString(cb);
    } else if (isNumeric(cb)) {
        t = getNumeric(cb);
    } else if (isBoolean(cb)) {
        t = getBoolean(cb);
    } else if (isIdent(cb)) {
        t = getIdent(cb);
    } else
        t = null;
    if (consume && (t != null)) {
        int p1 = p0;
        if (!t.isLiteral())
            p1 += t.length();
        else
            p1 = cb.position();
        cb.position(p1);
        if (skipWhitespace) {
            if (isWhitespace(cb))
                getWhitespace(cb);
        }
    } else
        cb.position(p0);
    return t;
}
 
Example 19
Source File: RestartOptions.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static Token getNumeric(CharBuffer cb, boolean consume) {
    int i = 0;                      // index from cb[position]
    int j;                          // helper index
    int k;                          // helper index
    int n = cb.remaining();         // # indices remaining
    int m = 0;                      // # digits in mantissa
    int e = 0;                      // # digits in exponent
    char c;
    StringBuffer sb = new StringBuffer();

    // integral component
    j = i;
    c = (i < n) ? cb.charAt(i) : 0;
    if (c == '0') {
        sb.append(c);
        ++i;
    } else if ((c >= '1') && (c <= '9')) {
        sb.append(c);
        ++i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    m += j - i;

    // fractional component
    j = i;
    c = (i < n) ? cb.charAt(i) : 0;
    if (c == '.') {
        sb.append(c);
        ++i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
    }
    m += j - i;

    // ensure mantissa is non-empty
    if (m == 0)
        return null;

    // exponent component
    k = sb.length();
    c = (i < n) ? cb.charAt(i) : 0;
    if ((c == 'e') || (c == 'E')) {
        sb.append('E');
        ++i;
        c = (i < n) ? cb.charAt(i) : 0;
        if ((c == '+') || (c == '-')) {
            sb.append(c);
            ++i;
        }
        j = i;
        while (i < n) {
            c = cb.charAt(i);
            if ((c >= '0') && (c <= '9')) {
                sb.append(c);
                ++i;
            } else
                break;
        }
        e += j - i;
    }
    if (e == 0)
        sb.setLength(k);

    if (sb.length() > 0) {
        if (consume)
            cb.position(cb.position() + i);
        return new Token(Token.Type.NUMERIC, sb.toString());
    } else
        return null;
}
 
Example 20
Source File: RestartOptions.java    From ttt with BSD 2-Clause "Simplified" License 4 votes vote down vote up
private static Token getToken(CharBuffer cb, boolean consume, boolean skipWhitespace) {
    Token t;
    int   p0 = cb.position();
    int   n  = cb.remaining();
    char  c1 = (n > 0) ? cb.charAt(0) : 0;
    // the following clauses are order dependent
    if (c1 == 0) {
        t = Token.EOS;
    } else if (c1 == '{') {
        t = Token.OPEN;
    } else if (c1 == '}') {
        t = Token.CLOSE;
    } else if (c1 == ':') {
        t = Token.COLON;
    } else if (c1 == ';') {
        t = Token.SEMICOLON;
    } else if (isString(cb)) {
        t = getString(cb);
    } else if (isNumeric(cb)) {
        t = getNumeric(cb);
    } else if (isBoolean(cb)) {
        t = getBoolean(cb);
    } else if (isUndefined(cb)) {
        t = getUndefined(cb);
    } else if (isIdent(cb)) {
        t = getIdent(cb);
    } else
        t = null;
    if (consume && (t != null)) {
        int p1 = p0;
        if (!t.isLiteral())
            p1 += t.length();
        else
            p1 = cb.position();
        cb.position(p1);
        if (skipWhitespace) {
            if (isWhitespace(cb))
                getWhitespace(cb);
        }
    } else
        cb.position(p0);
    return t;
}