jdk/src/share/classes/java/io/BufferedReader.java
author forax
Sun, 19 Feb 2012 16:51:35 +0000
changeset 11907 c021db66251d
parent 10347 1c9efe1ec7d3
child 17433 24c57ce3fec4
permissions -rw-r--r--
7146152: File.path should be final Reviewed-by: alanb, dholmes, mduigou
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8540
diff changeset
     2
 * Copyright (c) 1996, 2011, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Reads text from a character-input stream, buffering characters so as to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * provide for the efficient reading of characters, arrays, and lines.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * <p> The buffer size may be specified, or the default size may be used.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * default is large enough for most purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <p> In general, each read request made of a Reader causes a corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * read request to be made of the underlying character or byte stream.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * therefore advisable to wrap a BufferedReader around any Reader whose read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * operations may be costly, such as FileReaders and InputStreamReaders.  For
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * example,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * BufferedReader in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *   = new BufferedReader(new FileReader("foo.in"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * will buffer the input from the specified file.  Without buffering, each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * invocation of read() or readLine() could cause bytes to be read from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * file, converted into characters, and then returned, which can be very
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * inefficient.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * <p> Programs that use DataInputStreams for textual input can be localized by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * replacing each DataInputStream with an appropriate BufferedReader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see FileReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * @see InputStreamReader
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
    57
 * @see java.nio.file.Files#newBufferedReader
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
public class BufferedReader extends Reader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private Reader in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    private char cb[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private int nChars, nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static final int INVALIDATED = -2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private static final int UNMARKED = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private int markedChar = UNMARKED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private int readAheadLimit = 0; /* Valid only when markedChar > 0 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /** If the next character is a line feed, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private boolean skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /** The skipLF flag when the mark was set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private boolean markedSkipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static int defaultCharBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static int defaultExpectedLineLength = 80;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Creates a buffering character-input stream that uses an input buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * the specified size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @param  in   A Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * @param  sz   Input-buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @exception  IllegalArgumentException  If sz is <= 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    public BufferedReader(Reader in, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (sz <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new IllegalArgumentException("Buffer size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.in = in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        cb = new char[sz];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        nextChar = nChars = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * Creates a buffering character-input stream that uses a default-sized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * input buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @param  in   A Reader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public BufferedReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        this(in, defaultCharBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /** Checks to make sure that the stream has not been closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Fills the input buffer, taking the mark into account if it is valid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private void fill() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        int dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        if (markedChar <= UNMARKED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            /* No mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            dst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            /* Marked */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            int delta = nextChar - markedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            if (delta >= readAheadLimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                /* Gone past read-ahead limit: Invalidate mark */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                markedChar = INVALIDATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                readAheadLimit = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                dst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                if (readAheadLimit <= cb.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                    /* Shuffle in the current buffer */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                    System.arraycopy(cb, markedChar, cb, 0, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    markedChar = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                    dst = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                    /* Reallocate buffer to accommodate read-ahead limit */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                    char ncb[] = new char[readAheadLimit];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    System.arraycopy(cb, markedChar, ncb, 0, delta);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                    cb = ncb;
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
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                nextChar = nChars = delta;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        int n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            n = in.read(cb, dst, cb.length - dst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        } while (n == 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            nChars = dst + n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            nextChar = dst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Reads a single character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return The character read, as an integer in the range
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *         0 to 65535 (<tt>0x00-0xffff</tt>), or -1 if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     *         end of the stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                if (nextChar >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                    if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                        return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                return cb[nextChar++];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * Reads characters into a portion of an array, reading from the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * stream if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private int read1(char[] cbuf, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        if (nextChar >= nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
            /* If the requested length is at least as large as the buffer, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
               if there is no mark/reset activity, and if line feeds are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
               being skipped, do not bother to copy the characters into the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
               local buffer.  In this way buffered streams will cascade
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
               harmlessly. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (len >= cb.length && markedChar <= UNMARKED && !skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                return in.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        if (nextChar >= nChars) return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                    return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        int n = Math.min(len, nChars - nextChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        System.arraycopy(cb, nextChar, cbuf, off, n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        nextChar += n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * Reads characters into a portion of an array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p> This method implements the general contract of the corresponding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <code>{@link Reader#read(char[], int, int) read}</code> method of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>{@link Reader}</code> class.  As an additional convenience, it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * attempts to read as many characters as possible by repeatedly invoking
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * the <code>read</code> method of the underlying stream.  This iterated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <code>read</code> continues until one of the following conditions becomes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * true: <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *   <li> The specified number of characters have been read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *   <li> The <code>read</code> method of the underlying stream returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     *   <code>-1</code>, indicating end-of-file, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *   <li> The <code>ready</code> method of the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *   returns <code>false</code>, indicating that further input requests
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *   would block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * </ul> If the first <code>read</code> on the underlying stream returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * <code>-1</code> to indicate end-of-file then this method returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <code>-1</code>.  Otherwise this method returns the number of characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * actually read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p> Subclasses of this class are encouraged, but not required, to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * attempt to read as many characters as possible in the same fashion.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p> Ordinarily this method takes characters from this stream's character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * buffer, filling it from the underlying stream as necessary.  If,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * however, the buffer is empty, the mark is not valid, and the requested
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * length is at least as large as the buffer, then this method will read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * characters directly from the underlying stream into the given array.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Thus redundant <code>BufferedReader</code>s will not copy data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * unnecessarily.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @param      cbuf  Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * @param      off   Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param      len   Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @return     The number of characters read, or -1 if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if ((off < 0) || (off > cbuf.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                ((off + len) > cbuf.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            int n = read1(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            if (n <= 0) return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            while ((n < len) && in.ready()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                int n1 = read1(cbuf, off + n, len - n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                if (n1 <= 0) break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                n += n1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * Reads a line of text.  A line is considered to be terminated by any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * followed immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param      ignoreLF  If true, the next '\n' will be skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @return     A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *             any line-termination characters, or null if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see        java.io.LineNumberReader#readLine()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    String readLine(boolean ignoreLF) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        StringBuffer s = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        int startChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            boolean omitLF = ignoreLF || skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        bufferLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            for (;;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                if (nextChar >= nChars) { /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    if (s != null && s.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                        return s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                    else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                boolean eol = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                /* Skip a leftover '\n', if necessary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                if (omitLF && (cb[nextChar] == '\n'))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                    nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                omitLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            charLoop:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                for (i = nextChar; i < nChars; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                    c = cb[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    if ((c == '\n') || (c == '\r')) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                        eol = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
                        break charLoop;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                startChar = nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                nextChar = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                if (eol) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                    String str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                    if (s == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                        str = new String(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                        s.append(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                        str = s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                    if (c == '\r') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                        skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                    return str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                if (s == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    s = new StringBuffer(defaultExpectedLineLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                s.append(cb, startChar, i - startChar);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Reads a line of text.  A line is considered to be terminated by any one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * followed immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * @return     A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *             any line-termination characters, or null if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *             stream has been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @exception  IOException  If an I/O error occurs
8158
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   378
     *
77d9c0f1c19f 7006126: (fs) Updates to file system API (1/2011)
alanb
parents: 5506
diff changeset
   379
     * @see java.nio.file.Files#readAllLines
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        return readLine(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * Skips characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     * @param  n  The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * @return    The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @exception  IllegalArgumentException  If <code>n</code> is negative.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        if (n < 0L) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            throw new IllegalArgumentException("skip value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            long r = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            while (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                if (nextChar >= nChars)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                if (nextChar >= nChars) /* EOF */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    if (cb[nextChar] == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
                long d = nChars - nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
                if (r <= d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                    nextChar += r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                    r = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
                    r -= d;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
                    nextChar = nChars;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            return n - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Tells whether this stream is ready to be read.  A buffered character
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * stream is ready if the buffer is not empty, or if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * character stream is ready.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public boolean ready() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
             * If newline needs to be skipped and the next char to be read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
             * is a newline character, then just skip it right away.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
                /* Note that in.ready() will return true if and only if the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
                 * read on the stream will not block.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                if (nextChar >= nChars && in.ready()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                    fill();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
                if (nextChar < nChars) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                    if (cb[nextChar] == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                        nextChar++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            return (nextChar < nChars) || in.ready();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * Tells whether this stream supports the mark() operation, which it does.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        return true;
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
     * Marks the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * will attempt to reposition the stream to this point.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * @param readAheadLimit   Limit on the number of characters that may be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     *                         read while still preserving the mark. An attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *                         to reset the stream after reading characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *                         up to this limit or beyond may fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *                         A limit value larger than the size of the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     *                         buffer will cause a new buffer to be allocated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     *                         whose size is no smaller than limit.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *                         Therefore large values should be used with care.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @exception  IllegalArgumentException  If readAheadLimit is < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * @exception  IOException  If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        if (readAheadLimit < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throw new IllegalArgumentException("Read-ahead limit < 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            this.readAheadLimit = readAheadLimit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            markedChar = nextChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            markedSkipLF = skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * Resets the stream to the most recent mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * @exception  IOException  If the stream has never been marked,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     *                          or if the mark has been invalidated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
            if (markedChar < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                throw new IOException((markedChar == INVALIDATED)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
                                      ? "Mark invalid"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                                      : "Stream not marked");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            nextChar = markedChar;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
            skipLF = markedSkipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    public void close() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        synchronized (lock) {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   515
            if (in == null)
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   516
                return;
10347
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   517
            try {
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   518
                in.close();
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   519
            } finally {
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   520
                in = null;
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   521
                cb = null;
1c9efe1ec7d3 7015589: (spec) BufferedWriter.close leaves stream open if close of underlying Writer fails
alanb
parents: 9035
diff changeset
   522
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
}