0N/A/*
3909N/A * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.io;
0N/A
0N/A
0N/A/**
0N/A * Reads text from a character-input stream, buffering characters so as to
0N/A * provide for the efficient reading of characters, arrays, and lines.
0N/A *
0N/A * <p> The buffer size may be specified, or the default size may be used. The
0N/A * default is large enough for most purposes.
0N/A *
0N/A * <p> In general, each read request made of a Reader causes a corresponding
0N/A * read request to be made of the underlying character or byte stream. It is
0N/A * therefore advisable to wrap a BufferedReader around any Reader whose read()
0N/A * operations may be costly, such as FileReaders and InputStreamReaders. For
0N/A * example,
0N/A *
0N/A * <pre>
0N/A * BufferedReader in
0N/A * = new BufferedReader(new FileReader("foo.in"));
0N/A * </pre>
0N/A *
0N/A * will buffer the input from the specified file. Without buffering, each
0N/A * invocation of read() or readLine() could cause bytes to be read from the
0N/A * file, converted into characters, and then returned, which can be very
0N/A * inefficient.
0N/A *
0N/A * <p> Programs that use DataInputStreams for textual input can be localized by
0N/A * replacing each DataInputStream with an appropriate BufferedReader.
0N/A *
0N/A * @see FileReader
0N/A * @see InputStreamReader
3471N/A * @see java.nio.file.Files#newBufferedReader
0N/A *
0N/A * @author Mark Reinhold
0N/A * @since JDK1.1
0N/A */
0N/A
0N/Apublic class BufferedReader extends Reader {
0N/A
0N/A private Reader in;
0N/A
0N/A private char cb[];
0N/A private int nChars, nextChar;
0N/A
0N/A private static final int INVALIDATED = -2;
0N/A private static final int UNMARKED = -1;
0N/A private int markedChar = UNMARKED;
0N/A private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
0N/A
0N/A /** If the next character is a line feed, skip it */
0N/A private boolean skipLF = false;
0N/A
0N/A /** The skipLF flag when the mark was set */
0N/A private boolean markedSkipLF = false;
0N/A
0N/A private static int defaultCharBufferSize = 8192;
0N/A private static int defaultExpectedLineLength = 80;
0N/A
0N/A /**
0N/A * Creates a buffering character-input stream that uses an input buffer of
0N/A * the specified size.
0N/A *
0N/A * @param in A Reader
0N/A * @param sz Input-buffer size
0N/A *
0N/A * @exception IllegalArgumentException If sz is <= 0
0N/A */
0N/A public BufferedReader(Reader in, int sz) {
0N/A super(in);
0N/A if (sz <= 0)
0N/A throw new IllegalArgumentException("Buffer size <= 0");
0N/A this.in = in;
0N/A cb = new char[sz];
0N/A nextChar = nChars = 0;
0N/A }
0N/A
0N/A /**
0N/A * Creates a buffering character-input stream that uses a default-sized
0N/A * input buffer.
0N/A *
0N/A * @param in A Reader
0N/A */
0N/A public BufferedReader(Reader in) {
0N/A this(in, defaultCharBufferSize);
0N/A }
0N/A
0N/A /** Checks to make sure that the stream has not been closed */
0N/A private void ensureOpen() throws IOException {
0N/A if (in == null)
0N/A throw new IOException("Stream closed");
0N/A }
0N/A
0N/A /**
0N/A * Fills the input buffer, taking the mark into account if it is valid.
0N/A */
0N/A private void fill() throws IOException {
0N/A int dst;
0N/A if (markedChar <= UNMARKED) {
0N/A /* No mark */
0N/A dst = 0;
0N/A } else {
0N/A /* Marked */
0N/A int delta = nextChar - markedChar;
0N/A if (delta >= readAheadLimit) {
0N/A /* Gone past read-ahead limit: Invalidate mark */
0N/A markedChar = INVALIDATED;
0N/A readAheadLimit = 0;
0N/A dst = 0;
0N/A } else {
0N/A if (readAheadLimit <= cb.length) {
0N/A /* Shuffle in the current buffer */
0N/A System.arraycopy(cb, markedChar, cb, 0, delta);
0N/A markedChar = 0;
0N/A dst = delta;
0N/A } else {
0N/A /* Reallocate buffer to accommodate read-ahead limit */
0N/A char ncb[] = new char[readAheadLimit];
0N/A System.arraycopy(cb, markedChar, ncb, 0, delta);
0N/A cb = ncb;
0N/A markedChar = 0;
0N/A dst = delta;
0N/A }
0N/A nextChar = nChars = delta;
0N/A }
0N/A }
0N/A
0N/A int n;
0N/A do {
0N/A n = in.read(cb, dst, cb.length - dst);
0N/A } while (n == 0);
0N/A if (n > 0) {
0N/A nChars = dst + n;
0N/A nextChar = dst;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads a single character.
0N/A *
0N/A * @return The character read, as an integer in the range
0N/A * 0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the
0N/A * end of the stream has been reached
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public int read() throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A for (;;) {
0N/A if (nextChar >= nChars) {
0N/A fill();
0N/A if (nextChar >= nChars)
0N/A return -1;
0N/A }
0N/A if (skipLF) {
0N/A skipLF = false;
0N/A if (cb[nextChar] == '\n') {
0N/A nextChar++;
0N/A continue;
0N/A }
0N/A }
0N/A return cb[nextChar++];
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads characters into a portion of an array, reading from the underlying
0N/A * stream if necessary.
0N/A */
0N/A private int read1(char[] cbuf, int off, int len) throws IOException {
0N/A if (nextChar >= nChars) {
0N/A /* If the requested length is at least as large as the buffer, and
0N/A if there is no mark/reset activity, and if line feeds are not
0N/A being skipped, do not bother to copy the characters into the
0N/A local buffer. In this way buffered streams will cascade
0N/A harmlessly. */
0N/A if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
0N/A return in.read(cbuf, off, len);
0N/A }
0N/A fill();
0N/A }
0N/A if (nextChar >= nChars) return -1;
0N/A if (skipLF) {
0N/A skipLF = false;
0N/A if (cb[nextChar] == '\n') {
0N/A nextChar++;
0N/A if (nextChar >= nChars)
0N/A fill();
0N/A if (nextChar >= nChars)
0N/A return -1;
0N/A }
0N/A }
0N/A int n = Math.min(len, nChars - nextChar);
0N/A System.arraycopy(cb, nextChar, cbuf, off, n);
0N/A nextChar += n;
0N/A return n;
0N/A }
0N/A
0N/A /**
0N/A * Reads characters into a portion of an array.
0N/A *
0N/A * <p> This method implements the general contract of the corresponding
0N/A * <code>{@link Reader#read(char[], int, int) read}</code> method of the
0N/A * <code>{@link Reader}</code> class. As an additional convenience, it
0N/A * attempts to read as many characters as possible by repeatedly invoking
0N/A * the <code>read</code> method of the underlying stream. This iterated
0N/A * <code>read</code> continues until one of the following conditions becomes
0N/A * true: <ul>
0N/A *
0N/A * <li> The specified number of characters have been read,
0N/A *
0N/A * <li> The <code>read</code> method of the underlying stream returns
0N/A * <code>-1</code>, indicating end-of-file, or
0N/A *
0N/A * <li> The <code>ready</code> method of the underlying stream
0N/A * returns <code>false</code>, indicating that further input requests
0N/A * would block.
0N/A *
0N/A * </ul> If the first <code>read</code> on the underlying stream returns
0N/A * <code>-1</code> to indicate end-of-file then this method returns
0N/A * <code>-1</code>. Otherwise this method returns the number of characters
0N/A * actually read.
0N/A *
0N/A * <p> Subclasses of this class are encouraged, but not required, to
0N/A * attempt to read as many characters as possible in the same fashion.
0N/A *
0N/A * <p> Ordinarily this method takes characters from this stream's character
0N/A * buffer, filling it from the underlying stream as necessary. If,
0N/A * however, the buffer is empty, the mark is not valid, and the requested
0N/A * length is at least as large as the buffer, then this method will read
0N/A * characters directly from the underlying stream into the given array.
0N/A * Thus redundant <code>BufferedReader</code>s will not copy data
0N/A * unnecessarily.
0N/A *
0N/A * @param cbuf Destination buffer
0N/A * @param off Offset at which to start storing characters
0N/A * @param len Maximum number of characters to read
0N/A *
0N/A * @return The number of characters read, or -1 if the end of the
0N/A * stream has been reached
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public int read(char cbuf[], int off, int len) throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A if ((off < 0) || (off > cbuf.length) || (len < 0) ||
0N/A ((off + len) > cbuf.length) || ((off + len) < 0)) {
0N/A throw new IndexOutOfBoundsException();
0N/A } else if (len == 0) {
0N/A return 0;
0N/A }
0N/A
0N/A int n = read1(cbuf, off, len);
0N/A if (n <= 0) return n;
0N/A while ((n < len) && in.ready()) {
0N/A int n1 = read1(cbuf, off + n, len - n);
0N/A if (n1 <= 0) break;
0N/A n += n1;
0N/A }
0N/A return n;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads a line of text. A line is considered to be terminated by any one
0N/A * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
0N/A * followed immediately by a linefeed.
0N/A *
0N/A * @param ignoreLF If true, the next '\n' will be skipped
0N/A *
0N/A * @return A String containing the contents of the line, not including
0N/A * any line-termination characters, or null if the end of the
0N/A * stream has been reached
0N/A *
0N/A * @see java.io.LineNumberReader#readLine()
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A String readLine(boolean ignoreLF) throws IOException {
0N/A StringBuffer s = null;
0N/A int startChar;
0N/A
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A boolean omitLF = ignoreLF || skipLF;
0N/A
0N/A bufferLoop:
0N/A for (;;) {
0N/A
0N/A if (nextChar >= nChars)
0N/A fill();
0N/A if (nextChar >= nChars) { /* EOF */
0N/A if (s != null && s.length() > 0)
0N/A return s.toString();
0N/A else
0N/A return null;
0N/A }
0N/A boolean eol = false;
0N/A char c = 0;
0N/A int i;
0N/A
0N/A /* Skip a leftover '\n', if necessary */
0N/A if (omitLF && (cb[nextChar] == '\n'))
0N/A nextChar++;
0N/A skipLF = false;
0N/A omitLF = false;
0N/A
0N/A charLoop:
0N/A for (i = nextChar; i < nChars; i++) {
0N/A c = cb[i];
0N/A if ((c == '\n') || (c == '\r')) {
0N/A eol = true;
0N/A break charLoop;
0N/A }
0N/A }
0N/A
0N/A startChar = nextChar;
0N/A nextChar = i;
0N/A
0N/A if (eol) {
0N/A String str;
0N/A if (s == null) {
0N/A str = new String(cb, startChar, i - startChar);
0N/A } else {
0N/A s.append(cb, startChar, i - startChar);
0N/A str = s.toString();
0N/A }
0N/A nextChar++;
0N/A if (c == '\r') {
0N/A skipLF = true;
0N/A }
0N/A return str;
0N/A }
0N/A
0N/A if (s == null)
0N/A s = new StringBuffer(defaultExpectedLineLength);
0N/A s.append(cb, startChar, i - startChar);
0N/A }
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Reads a line of text. A line is considered to be terminated by any one
0N/A * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
0N/A * followed immediately by a linefeed.
0N/A *
0N/A * @return A String containing the contents of the line, not including
0N/A * any line-termination characters, or null if the end of the
0N/A * stream has been reached
0N/A *
0N/A * @exception IOException If an I/O error occurs
3471N/A *
3471N/A * @see java.nio.file.Files#readAllLines
0N/A */
0N/A public String readLine() throws IOException {
0N/A return readLine(false);
0N/A }
0N/A
0N/A /**
0N/A * Skips characters.
0N/A *
0N/A * @param n The number of characters to skip
0N/A *
0N/A * @return The number of characters actually skipped
0N/A *
0N/A * @exception IllegalArgumentException If <code>n</code> is negative.
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public long skip(long n) throws IOException {
0N/A if (n < 0L) {
0N/A throw new IllegalArgumentException("skip value is negative");
0N/A }
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A long r = n;
0N/A while (r > 0) {
0N/A if (nextChar >= nChars)
0N/A fill();
0N/A if (nextChar >= nChars) /* EOF */
0N/A break;
0N/A if (skipLF) {
0N/A skipLF = false;
0N/A if (cb[nextChar] == '\n') {
0N/A nextChar++;
0N/A }
0N/A }
0N/A long d = nChars - nextChar;
0N/A if (r <= d) {
0N/A nextChar += r;
0N/A r = 0;
0N/A break;
0N/A }
0N/A else {
0N/A r -= d;
0N/A nextChar = nChars;
0N/A }
0N/A }
0N/A return n - r;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tells whether this stream is ready to be read. A buffered character
0N/A * stream is ready if the buffer is not empty, or if the underlying
0N/A * character stream is ready.
0N/A *
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public boolean ready() throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A
0N/A /*
0N/A * If newline needs to be skipped and the next char to be read
0N/A * is a newline character, then just skip it right away.
0N/A */
0N/A if (skipLF) {
0N/A /* Note that in.ready() will return true if and only if the next
0N/A * read on the stream will not block.
0N/A */
0N/A if (nextChar >= nChars && in.ready()) {
0N/A fill();
0N/A }
0N/A if (nextChar < nChars) {
0N/A if (cb[nextChar] == '\n')
0N/A nextChar++;
0N/A skipLF = false;
0N/A }
0N/A }
0N/A return (nextChar < nChars) || in.ready();
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Tells whether this stream supports the mark() operation, which it does.
0N/A */
0N/A public boolean markSupported() {
0N/A return true;
0N/A }
0N/A
0N/A /**
0N/A * Marks the present position in the stream. Subsequent calls to reset()
0N/A * will attempt to reposition the stream to this point.
0N/A *
0N/A * @param readAheadLimit Limit on the number of characters that may be
0N/A * read while still preserving the mark. An attempt
0N/A * to reset the stream after reading characters
0N/A * up to this limit or beyond may fail.
0N/A * A limit value larger than the size of the input
0N/A * buffer will cause a new buffer to be allocated
0N/A * whose size is no smaller than limit.
0N/A * Therefore large values should be used with care.
0N/A *
0N/A * @exception IllegalArgumentException If readAheadLimit is < 0
0N/A * @exception IOException If an I/O error occurs
0N/A */
0N/A public void mark(int readAheadLimit) throws IOException {
0N/A if (readAheadLimit < 0) {
0N/A throw new IllegalArgumentException("Read-ahead limit < 0");
0N/A }
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A this.readAheadLimit = readAheadLimit;
0N/A markedChar = nextChar;
0N/A markedSkipLF = skipLF;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Resets the stream to the most recent mark.
0N/A *
0N/A * @exception IOException If the stream has never been marked,
0N/A * or if the mark has been invalidated
0N/A */
0N/A public void reset() throws IOException {
0N/A synchronized (lock) {
0N/A ensureOpen();
0N/A if (markedChar < 0)
0N/A throw new IOException((markedChar == INVALIDATED)
0N/A ? "Mark invalid"
0N/A : "Stream not marked");
0N/A nextChar = markedChar;
0N/A skipLF = markedSkipLF;
0N/A }
0N/A }
0N/A
0N/A public void close() throws IOException {
0N/A synchronized (lock) {
3643N/A if (in == null)
3643N/A return;
3643N/A in.close();
3643N/A in = null;
3643N/A cb = null;
0N/A }
0N/A }
0N/A}