Reads data from the ByteBuffer, inserting it into the StringBuffer, until a full line is read. - Java java.nio

Java examples for java.nio:ByteBuffer Read

Description

Reads data from the ByteBuffer, inserting it into the StringBuffer, until a full line is read.

Demo Code


//package com.java2s;
import java.nio.ByteBuffer;

public class Main {
    /**/*from  w  w w .  j a v  a2  s.  com*/
     * Reads data from the ByteBuffer, inserting it into the StringBuffer,
     * until a full line is read.  Returns true if a full line is read, false
     * if more data needs to be inserted into the buffer until a full line
     * can be read.
     */
    public static boolean readLine(ByteBuffer buffer, StringBuffer sBuffer) {
        int c = -1; //the character just read        
        while (buffer.hasRemaining()) {
            c = buffer.get();
            switch (c) {
            // if this was a \n character, we're done.
            case '\n':
                return true;
                // if this was a \r character, ignore it.
            case '\r':
                continue;
                // if it was any other character, append it to the buffer.
            default:
                sBuffer.append((char) c);
            }
        }

        return false;
    }
}

Related Tutorials