Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
31 * Reads text from a character-input stream, buffering characters so as to
32 * provide for the efficient reading of characters, arrays, and lines.
34 * <p> The buffer size may be specified, or the default size may be used. The
35 * default is large enough for most purposes.
37 * <p> In general, each read request made of a Reader causes a corresponding
38 * read request to be made of the underlying character or byte stream. It is
39 * therefore advisable to wrap a BufferedReader around any Reader whose read()
40 * operations may be costly, such as FileReaders and InputStreamReaders. For
45 * = new BufferedReader(new FileReader("foo.in"));
48 * will buffer the input from the specified file. Without buffering, each
49 * invocation of read() or readLine() could cause bytes to be read from the
50 * file, converted into characters, and then returned, which can be very
53 * <p> Programs that use DataInputStreams for textual input can be localized by
54 * replacing each DataInputStream with an appropriate BufferedReader.
57 * @see InputStreamReader
58 * @see java.nio.file.Files#newBufferedReader
60 * @author Mark Reinhold
64 public class BufferedReader extends Reader {
69 private int nChars, nextChar;
71 private static final int INVALIDATED = -2;
72 private static final int UNMARKED = -1;
73 private int markedChar = UNMARKED;
74 private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
76 /** If the next character is a line feed, skip it */
77 private boolean skipLF = false;
79 /** The skipLF flag when the mark was set */
80 private boolean markedSkipLF = false;
82 private static int defaultCharBufferSize = 8192;
83 private static int defaultExpectedLineLength = 80;
86 * Creates a buffering character-input stream that uses an input buffer of
90 * @param sz Input-buffer size
92 * @exception IllegalArgumentException If sz is <= 0
94 public BufferedReader(Reader in, int sz) {
97 throw new IllegalArgumentException("Buffer size <= 0");
100 nextChar = nChars = 0;
104 * Creates a buffering character-input stream that uses a default-sized
109 public BufferedReader(Reader in) {
110 this(in, defaultCharBufferSize);
113 /** Checks to make sure that the stream has not been closed */
114 private void ensureOpen() throws IOException {
116 throw new IOException("Stream closed");
120 * Fills the input buffer, taking the mark into account if it is valid.
122 private void fill() throws IOException {
124 if (markedChar <= UNMARKED) {
129 int delta = nextChar - markedChar;
130 if (delta >= readAheadLimit) {
131 /* Gone past read-ahead limit: Invalidate mark */
132 markedChar = INVALIDATED;
136 if (readAheadLimit <= cb.length) {
137 /* Shuffle in the current buffer */
138 System.arraycopy(cb, markedChar, cb, 0, delta);
142 /* Reallocate buffer to accommodate read-ahead limit */
143 char ncb[] = new char[readAheadLimit];
144 System.arraycopy(cb, markedChar, ncb, 0, delta);
149 nextChar = nChars = delta;
155 n = in.read(cb, dst, cb.length - dst);
164 * Reads a single character.
166 * @return The character read, as an integer in the range
167 * 0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the
168 * end of the stream has been reached
169 * @exception IOException If an I/O error occurs
171 public int read() throws IOException {
172 synchronized (lock) {
175 if (nextChar >= nChars) {
177 if (nextChar >= nChars)
182 if (cb[nextChar] == '\n') {
187 return cb[nextChar++];
193 * Reads characters into a portion of an array, reading from the underlying
194 * stream if necessary.
196 private int read1(char[] cbuf, int off, int len) throws IOException {
197 if (nextChar >= nChars) {
198 /* If the requested length is at least as large as the buffer, and
199 if there is no mark/reset activity, and if line feeds are not
200 being skipped, do not bother to copy the characters into the
201 local buffer. In this way buffered streams will cascade
203 if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
204 return in.read(cbuf, off, len);
208 if (nextChar >= nChars) return -1;
211 if (cb[nextChar] == '\n') {
213 if (nextChar >= nChars)
215 if (nextChar >= nChars)
219 int n = Math.min(len, nChars - nextChar);
220 System.arraycopy(cb, nextChar, cbuf, off, n);
226 * Reads characters into a portion of an array.
228 * <p> This method implements the general contract of the corresponding
229 * <code>{@link Reader#read(char[], int, int) read}</code> method of the
230 * <code>{@link Reader}</code> class. As an additional convenience, it
231 * attempts to read as many characters as possible by repeatedly invoking
232 * the <code>read</code> method of the underlying stream. This iterated
233 * <code>read</code> continues until one of the following conditions becomes
236 * <li> The specified number of characters have been read,
238 * <li> The <code>read</code> method of the underlying stream returns
239 * <code>-1</code>, indicating end-of-file, or
241 * <li> The <code>ready</code> method of the underlying stream
242 * returns <code>false</code>, indicating that further input requests
245 * </ul> If the first <code>read</code> on the underlying stream returns
246 * <code>-1</code> to indicate end-of-file then this method returns
247 * <code>-1</code>. Otherwise this method returns the number of characters
250 * <p> Subclasses of this class are encouraged, but not required, to
251 * attempt to read as many characters as possible in the same fashion.
253 * <p> Ordinarily this method takes characters from this stream's character
254 * buffer, filling it from the underlying stream as necessary. If,
255 * however, the buffer is empty, the mark is not valid, and the requested
256 * length is at least as large as the buffer, then this method will read
257 * characters directly from the underlying stream into the given array.
258 * Thus redundant <code>BufferedReader</code>s will not copy data
261 * @param cbuf Destination buffer
262 * @param off Offset at which to start storing characters
263 * @param len Maximum number of characters to read
265 * @return The number of characters read, or -1 if the end of the
266 * stream has been reached
268 * @exception IOException If an I/O error occurs
270 public int read(char cbuf[], int off, int len) throws IOException {
271 synchronized (lock) {
273 if ((off < 0) || (off > cbuf.length) || (len < 0) ||
274 ((off + len) > cbuf.length) || ((off + len) < 0)) {
275 throw new IndexOutOfBoundsException();
276 } else if (len == 0) {
280 int n = read1(cbuf, off, len);
281 if (n <= 0) return n;
282 while ((n < len) && in.ready()) {
283 int n1 = read1(cbuf, off + n, len - n);
292 * Reads a line of text. A line is considered to be terminated by any one
293 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
294 * followed immediately by a linefeed.
296 * @param ignoreLF If true, the next '\n' will be skipped
298 * @return A String containing the contents of the line, not including
299 * any line-termination characters, or null if the end of the
300 * stream has been reached
302 * @see java.io.LineNumberReader#readLine()
304 * @exception IOException If an I/O error occurs
306 String readLine(boolean ignoreLF) throws IOException {
307 StringBuffer s = null;
310 synchronized (lock) {
312 boolean omitLF = ignoreLF || skipLF;
317 if (nextChar >= nChars)
319 if (nextChar >= nChars) { /* EOF */
320 if (s != null && s.length() > 0)
329 /* Skip a leftover '\n', if necessary */
330 if (omitLF && (cb[nextChar] == '\n'))
336 for (i = nextChar; i < nChars; i++) {
338 if ((c == '\n') || (c == '\r')) {
344 startChar = nextChar;
350 str = new String(cb, startChar, i - startChar);
352 s.append(cb, startChar, i - startChar);
363 s = new StringBuffer(defaultExpectedLineLength);
364 s.append(cb, startChar, i - startChar);
370 * Reads a line of text. A line is considered to be terminated by any one
371 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
372 * followed immediately by a linefeed.
374 * @return A String containing the contents of the line, not including
375 * any line-termination characters, or null if the end of the
376 * stream has been reached
378 * @exception IOException If an I/O error occurs
380 * @see java.nio.file.Files#readAllLines
382 public String readLine() throws IOException {
383 return readLine(false);
389 * @param n The number of characters to skip
391 * @return The number of characters actually skipped
393 * @exception IllegalArgumentException If <code>n</code> is negative.
394 * @exception IOException If an I/O error occurs
396 public long skip(long n) throws IOException {
398 throw new IllegalArgumentException("skip value is negative");
400 synchronized (lock) {
404 if (nextChar >= nChars)
406 if (nextChar >= nChars) /* EOF */
410 if (cb[nextChar] == '\n') {
414 long d = nChars - nextChar;
430 * Tells whether this stream is ready to be read. A buffered character
431 * stream is ready if the buffer is not empty, or if the underlying
432 * character stream is ready.
434 * @exception IOException If an I/O error occurs
436 public boolean ready() throws IOException {
437 synchronized (lock) {
441 * If newline needs to be skipped and the next char to be read
442 * is a newline character, then just skip it right away.
445 /* Note that in.ready() will return true if and only if the next
446 * read on the stream will not block.
448 if (nextChar >= nChars && in.ready()) {
451 if (nextChar < nChars) {
452 if (cb[nextChar] == '\n')
457 return (nextChar < nChars) || in.ready();
462 * Tells whether this stream supports the mark() operation, which it does.
464 public boolean markSupported() {
469 * Marks the present position in the stream. Subsequent calls to reset()
470 * will attempt to reposition the stream to this point.
472 * @param readAheadLimit Limit on the number of characters that may be
473 * read while still preserving the mark. An attempt
474 * to reset the stream after reading characters
475 * up to this limit or beyond may fail.
476 * A limit value larger than the size of the input
477 * buffer will cause a new buffer to be allocated
478 * whose size is no smaller than limit.
479 * Therefore large values should be used with care.
481 * @exception IllegalArgumentException If readAheadLimit is < 0
482 * @exception IOException If an I/O error occurs
484 public void mark(int readAheadLimit) throws IOException {
485 if (readAheadLimit < 0) {
486 throw new IllegalArgumentException("Read-ahead limit < 0");
488 synchronized (lock) {
490 this.readAheadLimit = readAheadLimit;
491 markedChar = nextChar;
492 markedSkipLF = skipLF;
497 * Resets the stream to the most recent mark.
499 * @exception IOException If the stream has never been marked,
500 * or if the mark has been invalidated
502 public void reset() throws IOException {
503 synchronized (lock) {
506 throw new IOException((markedChar == INVALIDATED)
508 : "Stream not marked");
509 nextChar = markedChar;
510 skipLF = markedSkipLF;
514 public void close() throws IOException {
515 synchronized (lock) {