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

The following examples show how to use java.nio.CharBuffer#subSequence() . 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: ParseTesting.java    From nats.java with Apache License 2.0 6 votes vote down vote up
public static String grabNextWithSubsequence(CharBuffer buffer) {
    if (!buffer.hasRemaining()) {
        return null;
    }

    int start = buffer.position();

    while (buffer.hasRemaining()) {
        char c = buffer.get();

        if (c == ' ') {
            int end = buffer.position();
            buffer.position(start);
            CharBuffer slice = buffer.subSequence(0, end-start-1); //don't grab the space
            buffer.position(end);
            return slice.toString();
        }
    }

    buffer.position(start);
    String retVal = buffer.toString();
    buffer.position(buffer.limit());
    return retVal;
}
 
Example 2
Source File: ParseTesting.java    From nats.java with Apache License 2.0 6 votes vote down vote up
public static CharSequence grabNextAsSubsequence(CharBuffer buffer) {
    if (!buffer.hasRemaining()) {
        return null;
    }

    int start = buffer.position();

    while (buffer.hasRemaining()) {
        char c = buffer.get();

        if (c == ' ') {
            int end = buffer.position();
            buffer.position(start);
            CharBuffer slice = buffer.subSequence(0, end-start-1); //don't grab the space
            buffer.position(end);
            return slice;
        }
    }

    buffer.position(start);
    CharSequence retVal = buffer.subSequence(0, buffer.remaining());
    buffer.position(buffer.limit());
    return retVal;
}
 
Example 3
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCharBufferSubSequence() throws Exception {
    ByteBuffer b = ByteBuffer.allocateDirect(10).order(ByteOrder.nativeOrder());
    b.putChar('H');
    b.putChar('e');
    b.putChar('l');
    b.putChar('l');
    b.putChar('o');
    b.flip();

    assertEquals("Hello", b.asCharBuffer().toString());

    CharBuffer cb = b.asCharBuffer();
    CharSequence cs = cb.subSequence(0, cb.length());
    assertEquals("Hello", cs.toString());
}
 
Example 4
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 5
Source File: GerritConnection.java    From gerrit-events with MIT License 2 votes vote down vote up
/**
 *  Get sub sequence of buffer.
 *
 *  This method avoids error in java-api-check.
 *  animal-sniffer is confused by the signature of CharBuffer.subSequence()
 *  due to declaration of this method has been changed since Java7.
 *  (abstract -> non-abstract)
 *
 * @param cb a buffer
 * @param start start of sub sequence
 * @param end end of sub sequence
 * @return sub sequence.
 */
@IgnoreJRERequirement
private CharSequence getSubSequence(CharBuffer cb, int start, int end) {
    return cb.subSequence(start, end);
}