001    /*
002     * Copyright (C) 2007 Google Inc.
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    
017    package com.google.common.io;
018    
019    import com.google.common.annotations.Beta;
020    import com.google.common.base.Preconditions;
021    
022    import java.io.IOException;
023    import java.io.Reader;
024    import java.nio.CharBuffer;
025    import java.util.LinkedList;
026    import java.util.Queue;
027    
028    /**
029     * A class for reading lines of text. Provides the same functionality
030     * as {@link java.io.BufferedReader#readLine()} but for all {@link Readable}
031     * objects, not just instances of {@link Reader}.
032     *
033     * @author Chris Nokleberg
034     * @since 1
035     */
036    @Beta
037    public final class LineReader {
038      private final Readable readable;
039      private final Reader reader;
040      private final char[] buf = new char[0x1000]; // 4K
041      private final CharBuffer cbuf = CharBuffer.wrap(buf);
042    
043      private final Queue<String> lines = new LinkedList<String>();
044      private final LineBuffer lineBuf = new LineBuffer() {
045        @Override protected void handleLine(String line, String end) {
046          lines.add(line);
047        }
048      };
049    
050      /**
051       * Creates a new instance that will read lines from the given
052       * {@code Readable} object.
053       */
054      public LineReader(Readable readable) {
055        Preconditions.checkNotNull(readable);
056        this.readable = 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    }