jdk/src/share/classes/java/io/BufferedReader.java
author mduigou
Mon, 06 May 2013 20:54:54 -0700
changeset 17433 24c57ce3fec4
parent 10347 1c9efe1ec7d3
child 18156 edb590d448c5
permissions -rw-r--r--
8003258: BufferedReader.lines() Reviewed-by: alanb, mduigou, psandoz Contributed-by: Brian Goetz <brian.goetz@oracle.com>, Henry Jen <henry.jen@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17433
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
     2
 * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
17433
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    29
import java.util.Iterator;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    30
import java.util.NoSuchElementException;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    31
import java.util.Spliterator;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    32
import java.util.Spliterators;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    33
import java.util.stream.Stream;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    34
import java.util.stream.StreamSupport;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
    35
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Reads text from a character-input stream, buffering characters so as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * provide for the efficient reading of characters, arrays, and lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * <p> The buffer size may be specified, or the default size may be used.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * default is large enough for most purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <p> In general, each read request made of a Reader causes a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * read request to be made of the underlying character or byte stream.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * therefore advisable to wrap a BufferedReader around any Reader whose read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * operations may be costly, such as FileReaders and InputStreamReaders.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * BufferedReader in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *   = new BufferedReader(new FileReader("foo.in"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * will buffer the input from the specified file.  Without buffering, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * invocation of read() or readLine() could cause bytes to be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * file, converted into characters, and then returned, which can be very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * inefficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * <p> Programs that use DataInputStreams for textual input can be localized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * replacing each DataInputStream with an appropriate BufferedReader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * @see FileReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * @see InputStreamReader
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    64
 * @see java.nio.file.Files#newBufferedReader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
public class BufferedReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private Reader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private char cb[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private int nChars, nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int INVALIDATED = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int UNMARKED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private int markedChar = UNMARKED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /** If the next character is a line feed, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private boolean skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    /** The skipLF flag when the mark was set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private boolean markedSkipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private static int defaultCharBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static int defaultExpectedLineLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Creates a buffering character-input stream that uses an input buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param  in   A Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param  sz   Input-buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @exception  IllegalArgumentException  If sz is <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public BufferedReader(Reader in, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        if (sz <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        cb = new char[sz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        nextChar = nChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Creates a buffering character-input stream that uses a default-sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param  in   A Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public BufferedReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        this(in, defaultCharBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * Fills the input buffer, taking the mark into account if it is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private void fill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        int dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        if (markedChar <= UNMARKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            /* No mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            dst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            /* Marked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            int delta = nextChar - markedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            if (delta >= readAheadLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                /* Gone past read-ahead limit: Invalidate mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                markedChar = INVALIDATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                readAheadLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                dst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (readAheadLimit <= cb.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    /* Shuffle in the current buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    System.arraycopy(cb, markedChar, cb, 0, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    markedChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    dst = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    /* Reallocate buffer to accommodate read-ahead limit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    char ncb[] = new char[readAheadLimit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    System.arraycopy(cb, markedChar, ncb, 0, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    cb = ncb;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    markedChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                    dst = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                nextChar = nChars = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            n = in.read(cb, dst, cb.length - dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } while (n == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            nChars = dst + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            nextChar = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Reads a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * @return The character read, as an integer in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *         0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *         end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                if (nextChar >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                    if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                return cb[nextChar++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * Reads characters into a portion of an array, reading from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * stream if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private int read1(char[] cbuf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        if (nextChar >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            /* If the requested length is at least as large as the buffer, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
               if there is no mark/reset activity, and if line feeds are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
               being skipped, do not bother to copy the characters into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
               local buffer.  In this way buffered streams will cascade
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
               harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                return in.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (nextChar >= nChars) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        int n = Math.min(len, nChars - nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        System.arraycopy(cb, nextChar, cbuf, off, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        nextChar += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * Reads characters into a portion of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p> This method implements the general contract of the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <code>{@link Reader#read(char[], int, int) read}</code> method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * <code>{@link Reader}</code> class.  As an additional convenience, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * attempts to read as many characters as possible by repeatedly invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * the <code>read</code> method of the underlying stream.  This iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * <code>read</code> continues until one of the following conditions becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * true: <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *   <li> The specified number of characters have been read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *   <li> The <code>read</code> method of the underlying stream returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *   <code>-1</code>, indicating end-of-file, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *   <li> The <code>ready</code> method of the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *   returns <code>false</code>, indicating that further input requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *   would block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * </ul> If the first <code>read</code> on the underlying stream returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <code>-1</code> to indicate end-of-file then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <code>-1</code>.  Otherwise this method returns the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * actually read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * <p> Subclasses of this class are encouraged, but not required, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * attempt to read as many characters as possible in the same fashion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * <p> Ordinarily this method takes characters from this stream's character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * buffer, filling it from the underlying stream as necessary.  If,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * however, the buffer is empty, the mark is not valid, and the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * length is at least as large as the buffer, then this method will read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * characters directly from the underlying stream into the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Thus redundant <code>BufferedReader</code>s will not copy data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * unnecessarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @param      cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * @param      off   Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @param      len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @return     The number of characters read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            int n = read1(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (n <= 0) return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            while ((n < len) && in.ready()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                int n1 = read1(cbuf, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                if (n1 <= 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                n += n1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Reads a line of text.  A line is considered to be terminated by any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * followed immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param      ignoreLF  If true, the next '\n' will be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @return     A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     *             any line-termination characters, or null if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @see        java.io.LineNumberReader#readLine()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    String readLine(boolean ignoreLF) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        StringBuffer s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        int startChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            boolean omitLF = ignoreLF || skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        bufferLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                if (nextChar >= nChars) { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                    if (s != null && s.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                        return s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                boolean eol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                /* Skip a leftover '\n', if necessary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if (omitLF && (cb[nextChar] == '\n'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                omitLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            charLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                for (i = nextChar; i < nChars; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                    c = cb[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                    if ((c == '\n') || (c == '\r')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                        eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                        break charLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                startChar = nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                nextChar = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                if (eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        str = new String(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                        s.append(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                        str = s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                    s = new StringBuffer(defaultExpectedLineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                s.append(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * Reads a line of text.  A line is considered to be terminated by any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     * followed immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * @return     A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *             any line-termination characters, or null if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * @exception  IOException  If an I/O error occurs
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   385
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   386
     * @see java.nio.file.Files#readAllLines
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        return readLine(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * Skips characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @param  n  The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     * @return    The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (n < 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            throw new IllegalArgumentException("skip value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            long r = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            while (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                if (nextChar >= nChars) /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                long d = nChars - nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                if (r <= d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                    nextChar += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                    r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                    r -= d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    nextChar = nChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            return n - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * Tells whether this stream is ready to be read.  A buffered character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * stream is ready if the buffer is not empty, or if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * character stream is ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
             * If newline needs to be skipped and the next char to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
             * is a newline character, then just skip it right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                /* Note that in.ready() will return true if and only if the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                 * read on the stream will not block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                if (nextChar >= nChars && in.ready()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                if (nextChar < nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    if (cb[nextChar] == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            return (nextChar < nChars) || in.ready();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * Tells whether this stream supports the mark() operation, which it does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * will attempt to reposition the stream to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param readAheadLimit   Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *                         read while still preserving the mark. An attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     *                         to reset the stream after reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *                         up to this limit or beyond may fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *                         A limit value larger than the size of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *                         buffer will cause a new buffer to be allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     *                         whose size is no smaller than limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *                         Therefore large values should be used with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @exception  IllegalArgumentException  If readAheadLimit is < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
        if (readAheadLimit < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            throw new IllegalArgumentException("Read-ahead limit < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            this.readAheadLimit = readAheadLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
            markedChar = nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
            markedSkipLF = skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * Resets the stream to the most recent mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception  IOException  If the stream has never been marked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     *                          or if the mark has been invalidated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
            if (markedChar < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                throw new IOException((markedChar == INVALIDATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                                      ? "Mark invalid"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                                      : "Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            nextChar = markedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
            skipLF = markedSkipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        synchronized (lock) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   522
            if (in == null)
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   523
                return;
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   524
            try {
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   525
                in.close();
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   526
            } finally {
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   527
                in = null;
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   528
                cb = null;
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   529
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
    }
17433
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   532
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   533
    /**
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   534
     * Returns a {@code Stream}, the elements of which are lines read from
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   535
     * this {@code BufferedReader}.  The {@link Stream} is lazily populated,
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   536
     * i.e, read only occurs during the
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   537
     * <a href="../util/stream/package-summary.html#StreamOps">terminal
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   538
     * stream operation</a>.
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   539
     *
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   540
     * <p> The reader must not be operated on during the execution of the
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   541
     * terminal stream operation. Otherwise, the result of the terminal stream
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   542
     * operation is undefined.
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   543
     *
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   544
     * <p> After execution of the terminal stream operation there are no
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   545
     * guarantees that the reader will be at a specific position from which to
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   546
     * read the next character or line.
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   547
     *
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   548
     * <p> If an {@link IOException} is thrown when accessing the underlying
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   549
     * {@code BufferedReader}, it is wrapped in an {@link
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   550
     * UncheckedIOException} which will be thrown from the {@code Stream}
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   551
     * method that caused the read to take place. This method will return a
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   552
     * Stream if invoked on a BufferedReader that is closed. Any operation on
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   553
     * that stream requires reading from the BufferedReader after is it closed
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   554
     * will cause an UncheckedIOException to be thrown.
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   555
     *
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   556
     * @return a {@code Stream<String>} providing the lines of text
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   557
     *         described by this {@code BufferedReader}
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   558
     *
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   559
     * @since 1.8
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   560
     */
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   561
    public Stream<String> lines() {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   562
        Iterator<String> iter = new Iterator<String>() {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   563
            String nextLine = null;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   564
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   565
            @Override
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   566
            public boolean hasNext() {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   567
                if (nextLine != null) {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   568
                    return true;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   569
                } else {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   570
                    try {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   571
                        nextLine = readLine();
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   572
                        return (nextLine != null);
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   573
                    } catch (IOException e) {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   574
                        throw new UncheckedIOException(e);
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   575
                    }
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   576
                }
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   577
            }
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   578
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   579
            @Override
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   580
            public String next() {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   581
                if (nextLine != null || hasNext()) {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   582
                    String line = nextLine;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   583
                    nextLine = null;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   584
                    return line;
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   585
                } else {
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   586
                    throw new NoSuchElementException();
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   587
                }
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   588
            }
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   589
        };
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   590
        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iter, Spliterator.ORDERED));
24c57ce3fec4 8003258: BufferedReader.lines()
mduigou
parents: 10347
diff changeset
   591
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
}