001/*
002 * Copyright (C) 2007 The Guava Authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.google.common.io;
018
019import static com.google.common.base.Preconditions.checkNotNull;
020
021import com.google.common.annotations.Beta;
022
023import java.io.IOException;
024import java.io.Reader;
025import java.nio.CharBuffer;
026import java.util.LinkedList;
027import java.util.Queue;
028
029/**
030 * A class for reading lines of text. Provides the same functionality
031 * as {@link java.io.BufferedReader#readLine()} but for all {@link Readable}
032 * objects, not just instances of {@link Reader}.
033 *
034 * @author Chris Nokleberg
035 * @since 1.0
036 */
037@Beta
038public final class LineReader {
039  private final Readable readable;
040  private final Reader reader;
041  private final char[] buf = new char[0x1000]; // 4K
042  private final CharBuffer cbuf = CharBuffer.wrap(buf);
043
044  private final Queue<String> lines = new LinkedList<String>();
045  private final LineBuffer lineBuf = new LineBuffer() {
046    @Override protected void handleLine(String line, String end) {
047      lines.add(line);
048    }
049  };
050
051  /**
052   * Creates a new instance that will read lines from the given
053   * {@code Readable} object.
054   */
055  public LineReader(Readable readable) {
056    this.readable = checkNotNull(readable);
057    this.reader = (readable instanceof Reader) ? (Reader) readable : null;
058  }
059
060  /**
061   * Reads a line of text. A line is considered to be terminated by any
062   * one of a line feed ({@code '\n'}), a carriage return
063   * ({@code '\r'}), or a carriage return followed immediately by a linefeed
064   * ({@code "\r\n"}).
065   *
066   * @return a {@code String} containing the contents of the line, not
067   *     including any line-termination characters, or {@code null} if the
068   *     end of the stream has been reached.
069   * @throws IOException if an I/O error occurs
070   */
071  public String readLine() throws IOException {
072    while (lines.peek() == null) {
073      cbuf.clear();
074      // The default implementation of Reader#read(CharBuffer) allocates a
075      // temporary char[], so we call Reader#read(char[], int, int) instead.
076      int read = (reader != null)
077          ? reader.read(buf, 0, buf.length)
078          : readable.read(cbuf);
079      if (read == -1) {
080        lineBuf.finish();
081        break;
082      }
083      lineBuf.add(buf, 0, read);
084    }
085    return lines.poll();
086  }
087}