MCPcopy
hub / github.com/google/guava / LineReader

Class LineReader

guava/src/com/google/common/io/LineReader.java:38–85  ·  view source on GitHub ↗

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

Source from the content-addressed store, hash-verified

36 * @since 1.0
37 */
38@J2ktIncompatible
39@GwtIncompatible
40public final class LineReader {
41 private final Readable readable;
42 private final @Nullable Reader reader;
43 private final CharBuffer cbuf = createBuffer();
44 private final char[] buf = cbuf.array();
45
46 private final Queue<String> lines = new ArrayDeque<>();
47 private final LineBuffer lineBuf =
48 new LineBuffer() {
49 @Override
50 protected void handleLine(String line, String end) {
51 lines.add(line);
52 }
53 };
54
55 /** Creates a new instance that will read lines from the given {@code Readable} object. */
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 ({@code
63 * '\n'}), a carriage return ({@code '\r'}), or a carriage return followed immediately by a
64 * 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 @CanIgnoreReturnValue // to skip a line
71 public @Nullable String readLine() throws IOException {
72 while (lines.peek() == null) {
73 Java8Compatibility.clear(cbuf);
74 // The default implementation of Reader#read(CharBuffer) allocates a
75 // temporary char[], so we call Reader#read(char[], int, int) instead.
76 int read = (reader != null) ? reader.read(buf, 0, buf.length) : 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}

Callers

nothing calls this directly

Calls 1

createBufferMethod · 0.45

Tested by

no test coverage detected