Java CharBuffer searchNewline(CharBuffer buffer, int from)

Here you can find the source of searchNewline(CharBuffer buffer, int from)

Description

Scan for a newline sequence in the given CharBuffer, starting at position from until buffer.position().

License

Open Source License

Parameter

Parameter Description
buffer the character buffer
from the position to start searching

Return

the index of first newline sequence found, or -1

Declaration

public static int searchNewline(CharBuffer buffer, int from) 

Method Source Code


//package com.java2s;
/*//from   w  w  w  . j  ava 2s . c  o  m
 * Helma License Notice
 *
 * The contents of this file are subject to the Helma License
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://adele.invoker.org/download/invoker/license.txt
 *
 * Copyright 2005 Hannes Wallnoefer. All Rights Reserved.
 */

import java.nio.CharBuffer;

public class Main {
    /**
     * Scan for a newline sequence in the given CharBuffer, starting at
     * position from until buffer.position(). A newline sequence is one of
     * "r\n", "\r", or "\n". Returns the index of the
     * first newline sequence, or -1 if none was found.
     *
     * @param buffer the character buffer
     * @param from the position to start searching
     * @return the index of first newline sequence found, or -1
     */
    public static int searchNewline(CharBuffer buffer, int from) {
        int to = buffer.position();
        if (from >= to) {
            return -1;
        }
        char[] chars = buffer.array();
        for (int i = from; i < to; i++) {
            if (chars[i] == '\n' || (chars[i] == '\r' && i < to - 1)) {
                return i;
            }
        }
        return -1;
    }
}

Related

  1. getCharsFromBufferAndReset(CharBuffer buffer)
  2. nextEquals(final CharBuffer buffer, final char next)
  3. printBuffer(CharBuffer charBuffer)
  4. printCharBuffer(CharBuffer msg)
  5. put(char c, CharBuffer buffer)
  6. startsWith(final CharBuffer charBuffer, final String token)
  7. toCharArray(CharBuffer charBuffer)
  8. toCharBuffer(int[] codePoints)
  9. zeroCharBuffer(CharBuffer buf)