Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  * Copyright (C) 2007 The Guava Authors
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * See the License for the specific language governing permissions and
 * limitations under the License.
A class for reading lines of text. Provides the same functionality as but for all java.lang.Readable objects, not just instances of

Chris Nokleberg
public final class LineReader {
  private final Readable readable;
  private final Reader reader;
  private final char[] buf = new char[0x1000]; // 4K
  private final CharBuffer cbuf = CharBuffer.wrap();
  private final Queue<Stringlines = new LinkedList<String>();
  private final LineBuffer lineBuf = new LineBuffer() {
    @Override protected void handleLine(String lineString end) {

Creates a new instance that will read lines from the given Readable object.
  public LineReader(Readable readable) {
    this. = readable;
    this. = (readable instanceof Reader) ? (Readerreadable : null;

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed ("\r\n").

a String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached.
Throws: if an I/O error occurs
  public String readLine() throws IOException {
    while (.peek() == null) {
      // The default implementation of Reader#read(CharBuffer) allocates a
      // temporary char[], so we call Reader#read(char[], int, int) instead.
      int read = ( != null)
          ? .read(, 0, .)
          : .read();
      if (read == -1) {
      .add(, 0, read);
    return .poll();
New to GrepCode? Check out our FAQ X