1/*
2 * Copyright (C) 2007 The Guava Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.common.io;
18
19import com.google.common.annotations.Beta;
20import com.google.common.base.Preconditions;
21
22import java.io.IOException;
23import java.io.Reader;
24import java.nio.CharBuffer;
25import java.util.LinkedList;
26import java.util.Queue;
27
28/**
29 * A class for reading lines of text. Provides the same functionality
30 * as {@link java.io.BufferedReader#readLine()} but for all {@link Readable}
31 * objects, not just instances of {@link Reader}.
32 *
33 * @author Chris Nokleberg
34 * @since 1.0
35 */
36@Beta
37public final class LineReader {
38  private final Readable readable;
39  private final Reader reader;
40  private final char[] buf = new char[0x1000]; // 4K
41  private final CharBuffer cbuf = CharBuffer.wrap(buf);
42
43  private final Queue<String> lines = new LinkedList<String>();
44  private final LineBuffer lineBuf = new LineBuffer() {
45    @Override protected void handleLine(String line, String end) {
46      lines.add(line);
47    }
48  };
49
50  /**
51   * Creates a new instance that will read lines from the given
52   * {@code Readable} object.
53   */
54  public LineReader(Readable readable) {
55    Preconditions.checkNotNull(readable);
56    this.readable = readable;
57    this.reader = (readable instanceof Reader) ? (Reader) readable : null;
58  }
59
60  /**
61   * Reads a line of text. A line is considered to be terminated by any
62   * one of a line feed ({@code '\n'}), a carriage return
63   * ({@code '\r'}), or a carriage return followed immediately by a linefeed
64   * ({@code "\r\n"}).
65   *
66   * @return a {@code String} containing the contents of the line, not
67   *     including any line-termination characters, or {@code null} if the
68   *     end of the stream has been reached.
69   * @throws IOException if an I/O error occurs
70   */
71  public String readLine() throws IOException {
72    while (lines.peek() == null) {
73      cbuf.clear();
74      // The default implementation of Reader#read(CharBuffer) allocates a
75      // temporary char[], so we call Reader#read(char[], int, int) instead.
76      int read = (reader != null)
77          ? reader.read(buf, 0, buf.length)
78          : readable.read(cbuf);
79      if (read == -1) {
80        lineBuf.finish();
81        break;
82      }
83      lineBuf.add(buf, 0, read);
84    }
85    return lines.poll();
86  }
87}
88