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