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