• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Google Inc.
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 
17 package com.google.common.io;
18 
19 import com.google.common.base.Preconditions;
20 
21 import java.io.IOException;
22 import java.io.Reader;
23 import java.nio.CharBuffer;
24 import java.util.LinkedList;
25 import java.util.Queue;
26 
27 /**
28  * A class for reading lines of text. Provides the same functionality
29  * as {@link java.io.BufferedReader#readLine()} but for all {@link Readable}
30  * objects, not just instances of {@link Reader}.
31  *
32  * @author Chris Nokleberg
33  * @since 2009.09.15 <b>tentative</b>
34  */
35 public final class LineReader {
36   private final Readable readable;
37   private final Reader reader;
38   private final char[] buf = new char[0x1000]; // 4K
39   private final CharBuffer cbuf = CharBuffer.wrap(buf);
40 
41   private final Queue<String> lines = new LinkedList<String>();
42   private final LineBuffer lineBuf = new LineBuffer() {
43     @Override protected void handleLine(String line, String end) {
44       lines.add(line);
45     }
46   };
47 
48   /**
49    * Creates a new instance that will read lines from the given
50    * {@code Readable} object.
51    */
LineReader(Readable readable)52   public LineReader(Readable readable) {
53     Preconditions.checkNotNull(readable);
54     this.readable = readable;
55     this.reader = (readable instanceof Reader) ? (Reader) readable : null;
56   }
57 
58   /**
59    * Reads a line of text. A line is considered to be terminated by any
60    * one of a line feed ({@code '\n'}), a carriage return
61    * ({@code '\r'}), or a carriage return followed immediately by a linefeed
62    * ({@code "\r\n"}).
63    *
64    * @return a {@code String} containing the contents of the line, not
65    *     including any line-termination characters, or {@code null} if the
66    *     end of the stream has been reached.
67    * @throws IOException if an I/O error occurs
68    */
readLine()69   public String readLine() throws IOException {
70     while (lines.peek() == null) {
71       cbuf.clear();
72       // The default implementation of Reader#read(CharBuffer) allocates a
73       // temporary char[], so we call Reader#read(char[], int, int) instead.
74       int read = (reader != null)
75           ? reader.read(buf, 0, buf.length)
76           : readable.read(cbuf);
77       if (read == -1) {
78         lineBuf.finish();
79         break;
80       }
81       lineBuf.add(buf, 0, read);
82     }
83     return lines.poll();
84   }
85 }
86