Java StringBuilder.getChars Examples

Java StringBuilder.getChars - 15 examples found. These are the top rated real world Java examples of StringBuilder.getChars extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
  public char[] toCharArray() {
    int l = currentString.length();
    char[] buffer = new char[l];
    currentString.getChars(0, l, buffer, 0);

    return buffer;
  }
 /** @return Return the buffer's current value as a string. */
 @Nonnull
 @ReturnsMutableCopy
 public char[] getAsCharArray() {
   final int nChars = m_aSB.length();
   final char[] ret = new char[nChars];
   m_aSB.getChars(0, nChars, ret, 0);
   return ret;
 }
Example #3
0
  /**
   * Tag changes in the given <var>line1</var> and <var>line2</var> for highlighting. Removed parts
   * are tagged with CSS class {@code d}, new parts are tagged with CSS class {@code a} using a
   * {@code span} element.
   *
   * @param line1 line of the original file
   * @param line2 line of the changed/new file
   * @return the tagged lines (field[0] ~= line1, field[1] ~= line2).
   * @throws NullPointerException if one of the given parameters is {@code null}.
   */
  public static String[] diffline(StringBuilder line1, StringBuilder line2) {
    int m = line1.length();
    int n = line2.length();
    if (n == 0 || m == 0) {
      return new String[] {line1.toString(), line2.toString()};
    }

    int s = 0;
    char[] csl1 = new char[m + SPAN_LEN];
    line1.getChars(0, m--, csl1, 0);
    char[] csl2 = new char[n + SPAN_LEN];
    line2.getChars(0, n--, csl2, 0);
    while (s <= m && s <= n && csl1[s] == csl2[s]) {
      s++;
    }
    while (s <= m && s <= n && csl1[m] == csl2[n]) {
      m--;
      n--;
    }

    String[] ret = new String[2];
    // deleted
    if (s <= m) {
      m++;
      System.arraycopy(csl1, m, csl1, m + SPAN_LEN, line1.length() - m);
      System.arraycopy(csl1, s, csl1, s + SPAN_D.length(), m - s);
      SPAN_E.getChars(0, SPAN_E.length(), csl1, m + SPAN_D.length());
      SPAN_D.getChars(0, SPAN_D.length(), csl1, s);
      ret[0] = new String(csl1);
    } else {
      ret[0] = line1.toString();
    }
    // added
    if (s <= n) {
      n++;
      System.arraycopy(csl2, n, csl2, n + SPAN_LEN, line2.length() - n);
      System.arraycopy(csl2, s, csl2, s + SPAN_A.length(), n - s);
      SPAN_E.getChars(0, SPAN_E.length(), csl2, n + SPAN_A.length());
      SPAN_A.getChars(0, SPAN_A.length(), csl2, s);
      ret[1] = new String(csl2);
    } else {
      ret[1] = line2.toString();
    }
    return ret;
  }
    @Override
    public void flush() {
      char tmp[] = new char[str.length()];
      str.getChars(0, str.length() - 1, tmp, 0);
      String s = (new String(tmp));

      FormattedConsole console = FormattedConsole.getDefault();
      console.print(_level, s);

      str = new StringBuilder();
    }
 private void flushLineBuffer() throws IOException {
   int rest = lineBuffer.length();
   int cursor = 0;
   while (rest > 0) {
     int chunkSize = Math.min(rest, writeBuffer.length);
     lineBuffer.getChars(cursor, cursor + chunkSize, writeBuffer, 0);
     writer.write(writeBuffer, 0, chunkSize);
     rest -= chunkSize;
     cursor += chunkSize;
   }
   lineBuffer.setLength(0);
 }
  @Override
  public final boolean incrementToken() throws IOException {
    clearAttributes();
    if (delimitersCount == -1) {
      int length = 0;
      delimiterPositions.add(0);
      while (true) {
        int c = input.read();
        if (c < 0) {
          break;
        }
        length++;
        if (c == delimiter) {
          delimiterPositions.add(length);
          resultToken.append(replacement);
        } else {
          resultToken.append((char) c);
        }
      }
      delimitersCount = delimiterPositions.size();
      if (delimiterPositions.get(delimitersCount - 1) < length) {
        delimiterPositions.add(length);
        delimitersCount++;
      }
      if (resultTokenBuffer.length < resultToken.length()) {
        resultTokenBuffer = new char[resultToken.length()];
      }
      resultToken.getChars(0, resultToken.length(), resultTokenBuffer, 0);
      resultToken.setLength(0);
      int idx = delimitersCount - 1 - skip;
      if (idx >= 0) {
        // otherwise its ok, because we will skip and return false
        endPosition = delimiterPositions.get(idx);
      }
      finalOffset = correctOffset(length);
      posAtt.setPositionIncrement(1);
    } else {
      posAtt.setPositionIncrement(0);
    }

    while (skipped < delimitersCount - skip - 1) {
      int start = delimiterPositions.get(skipped);
      termAtt.copyBuffer(resultTokenBuffer, start, endPosition - start);
      offsetAtt.setOffset(correctOffset(start), correctOffset(endPosition));
      skipped++;
      return true;
    }

    return false;
  }