MCPcopy Index your code
hub / github.com/antlr/codebuff / LineReader

Class LineReader

output/java_guava/1.4.17/LineReader.java:38–87  ·  view source on GitHub ↗

A class for reading lines of text. Provides the same functionality as java.io.BufferedReader#readLine() but for all Readable objects, not just instances of Reader. @author Chris Nokleberg @since 1.0

Source from the content-addressed store, hash-verified

36
37
38@Beta
39@GwtIncompatible
40public final class LineReader {
41 private final Readable readable;
42 private final Reader reader;
43 private final CharBuffer cbuf = createBuffer();
44 private final char[] buf = cbuf.array();
45 private final Queue<String> lines = new LinkedList<String>();
46 private final LineBuffer lineBuf = new LineBuffer() {
47 @Override
48 protected void handleLine(String line, String end) {
49 lines.add(line);
50 }
51 };
52
53 /**
54 * Creates a new instance that will read lines from the given {@code Readable} object.
55 */
56 public LineReader(Readable readable) {
57 this.readable = checkNotNull(readable);
58 this.reader = (readable instanceof Reader) ? (Reader) readable : null;
59 }
60
61 /**
62 * Reads a line of text. A line is considered to be terminated by any one of a line feed
63 * ({@code '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by
64 * a linefeed ({@code "\r\n"}).
65 *
66 * @return a {@code String} containing the contents of the line, not including any
67 * line-termination characters, or {@code null} if the end of the stream has been reached.
68 * @throws IOException if an I/O error occurs
69 */
70
71 @CanIgnoreReturnValue // to skip a line
72 public String readLine() throws IOException {
73 while (lines.peek() == null) {
74 cbuf.clear();
75 // The default implementation of Reader#read(CharBuffer) allocates a
76 // temporary char[], so we call Reader#read(char[], int, int) instead.
77
78 int read = (reader != null) ? reader.read(buf, 0, buf.length) : 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}

Callers

nothing calls this directly

Calls 1

createBufferMethod · 0.45

Tested by

no test coverage detected