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