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    
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.0
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        
046        @Override
047        protected void handleLine(String line, String end) {
048          lines.add(line);
049        }
050      };
051    
052      /**
053       * Creates a new instance that will read lines from the given
054       * {@code Readable} object.
055       */
056      public LineReader(Readable readable) {
057        Preconditions.checkNotNull(readable);
058        this.readable = readable;
059        this.reader = (readable instanceof Reader) ? (Reader) readable : null;
060      }
061    
062      /**
063       * Reads a line of text. A line is considered to be terminated by any
064       * one of a line feed ({@code '\n'}), a carriage return
065       * ({@code '\r'}), or a carriage return followed immediately by a linefeed
066       * ({@code "\r\n"}).
067       *
068       * @return a {@code String} containing the contents of the line, not
069       *     including any line-termination characters, or {@code null} if the
070       *     end of the stream has been reached.
071       * @throws IOException if an I/O error occurs
072       */
073      public String readLine() throws IOException {
074        while (lines.peek() == null) {
075          cbuf.clear();
076          // The default implementation of Reader#read(CharBuffer) allocates a
077          // temporary char[], so we call Reader#read(char[], int, int) instead.
078          int read = (reader != null)
079              ? reader.read(buf, 0, buf.length)
080              : readable.read(cbuf);
081          if (read == -1) {
082            lineBuf.finish();
083            break;
084          }
085          lineBuf.add(buf, 0, read);
086        }
087        return lines.poll();
088      }
089    }