src/java.base/share/classes/java/io/LineNumberReader.java
author rriggs
Mon, 19 Jun 2017 17:38:33 -0400
changeset 47415 354a527f3246
parent 47216 71c04702a3d5
child 54643 3edf22a7cbaf
permissions -rw-r--r--
8181597: Process Proxy presentation Reviewed-by: dfuchs, ahgross, rhalade, skoivu
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 11121
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
 * A buffered character-input stream that keeps track of line numbers.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * class defines methods {@link #setLineNumber(int)} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * #getLineNumber()} for setting and getting the current line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> By default, line numbering begins at 0. This number increments at every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <a href="#lt">line terminator</a> as the data is read, and can be changed
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
    37
 * with a call to {@code setLineNumber(int)}.  Note however, that
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
    38
 * {@code setLineNumber(int)} does not actually change the current position in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the stream; it only changes the value that will be returned by
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
    40
 * {@code getLineNumber()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32033
diff changeset
    42
 * <p> A line is considered to be <a id="lt">terminated</a> by any one of a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * line feed ('\n'), a carriage return ('\r'), or a carriage return followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author      Mark Reinhold
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 14342
diff changeset
    47
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public class LineNumberReader extends BufferedReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /** The current line number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    private int lineNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /** The line number of the mark, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int markedLineNumber; // Defaults to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /** If the next character is a line feed, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private boolean skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    /** The skipLF flag when the mark was set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private boolean markedSkipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * Create a new line-numbering reader, using the default input-buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *         A Reader object to provide the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public LineNumberReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * Create a new line-numbering reader, reading characters into a buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *         A Reader object to provide the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param  sz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *         An int specifying the size of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    public LineNumberReader(Reader in, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        super(in, sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * Set the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param  lineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *         An int specifying the line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @see #getLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    public void setLineNumber(int lineNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.lineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Get the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return  The current line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see #setLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public int getLineNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * Read a single character.  <a href="#lt">Line terminators</a> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * compressed into single newline ('\n') characters.  Whenever a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * terminator is read the current line number is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return  The character read, or -1 if the end of the stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *          reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     */
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   123
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            int c = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    c = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            case '\n':          /* Fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Read characters into a portion of an array.  Whenever a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * href="#lt">line terminator</a> is read the current line number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param  cbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *         Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *         Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *         Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @return  The number of bytes read, or -1 if the end of the stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *          already been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     *          If an I/O error occurs
30443
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   162
     *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   163
     * @throws  IndexOutOfBoundsException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     */
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   165
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
            int n = super.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            for (int i = off; i < off + n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                int c = cbuf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                case '\n':      /* Fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
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 n;
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
     * Read a line of text.  Whenever a <a href="#lt">line terminator</a> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * read the current line number is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @return  A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *          any <a href="#lt">line termination characters</a>, or
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
   196
     *          {@code null} if the end of the stream has been reached
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            String l = super.readLine(skipLF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            if (l != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            return l;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /** Maximum skip-buffer size */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    private static final int maxSkipBufferSize = 8192;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    /** Skip buffer, null until allocated */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private char skipBuffer[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * Skip characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * @param  n
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *         The number of characters to skip
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @return  The number of characters actually skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @throws  IllegalArgumentException
32033
bf24e33c7919 8132468: docs: replace <tt> tags (obsolete in html5) for java.io, java.lang, java.math
avstepan
parents: 30443
diff changeset
   229
     *          If {@code n} is negative
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (n < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            throw new IllegalArgumentException("skip() value is negative");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        int nn = (int) Math.min(n, maxSkipBufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if ((skipBuffer == null) || (skipBuffer.length < nn))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                skipBuffer = new char[nn];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            long r = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            while (r > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                int nc = read(skipBuffer, 0, (int) Math.min(r, nn));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (nc == -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                r -= nc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return n - r;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Mark the present position in the stream.  Subsequent calls to reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * will attempt to reposition the stream to this point, and will also reset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * the line number appropriately.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param  readAheadLimit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *         Limit on the number of characters that may be read while still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *         preserving the mark.  After reading this many characters,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *         attempting to reset the stream may fail.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    public void mark(int readAheadLimit) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            super.mark(readAheadLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            markedLineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            markedSkipLF     = skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * Reset the stream to the most recent mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     *          If the stream has not been marked, or if the mark has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     *          invalidated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            lineNumber = markedLineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            skipLF     = markedSkipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
}