src/java.base/share/classes/java/io/LineNumberReader.java
author bpb
Wed, 11 Sep 2019 12:32:01 -0700
changeset 58089 e64fec9f1773
parent 54643 3edf22a7cbaf
permissions -rw-r--r--
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF Reviewed-by: rriggs, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
54643
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1996, 2019, 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
 * A buffered character-input stream that keeps track of line numbers.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * class defines methods {@link #setLineNumber(int)} and {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * #getLineNumber()} for setting and getting the current line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * respectively.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p> By default, line numbering begins at 0. This number increments at every
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <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
    36
 * 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
    37
 * {@code setLineNumber(int)} does not actually change the current position in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * 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
    39
 * {@code getLineNumber()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
44844
b2b4d98404ba 8179364: update "<a name=" in java.base module to use id attribute
jjg
parents: 32033
diff changeset
    41
 * <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
    42
 * line feed ('\n'), a carriage return ('\r'), or a carriage return followed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * immediately by a linefeed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @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
    46
 * @since       1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public class LineNumberReader extends BufferedReader {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /** The current line number */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    private int lineNumber = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /** The line number of the mark, if any */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private int markedLineNumber; // Defaults to 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /** If the next character is a line feed, skip it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private boolean skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /** The skipLF flag when the mark was set */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private boolean markedSkipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Create a new line-numbering reader, using the default input-buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *         A Reader object to provide the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public LineNumberReader(Reader in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Create a new line-numbering reader, reading characters into a buffer of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * the given size.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param  in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *         A Reader object to provide the underlying stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param  sz
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *         An int specifying the size of the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public LineNumberReader(Reader in, int sz) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        super(in, sz);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Set the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param  lineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *         An int specifying the line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @see #getLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    public void setLineNumber(int lineNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.lineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * Get the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @return  The current line number
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @see #setLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public int getLineNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        return lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * Read a single character.  <a href="#lt">Line terminators</a> are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * compressed into single newline ('\n') characters.  Whenever a line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * terminator is read the current line number is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return  The character read, or -1 if the end of the stream has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *          reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     */
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   122
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            int c = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    c = super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            case '\n':          /* Fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        }
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
     * Read characters into a portion of an array.  Whenever a <a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * href="#lt">line terminator</a> is read the current line number is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param  cbuf
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *         Destination buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param  off
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *         Offset at which to start storing characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param  len
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *         Maximum number of characters to read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @return  The number of bytes read, or -1 if the end of the stream has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     *          already been reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *          If an I/O error occurs
30443
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   161
     *
8d2f2ce637bd 8029689: (spec) Reader.read(char[], int, int) throws unspecified IndexOutOfBoundsException
prappo
parents: 25859
diff changeset
   162
     * @throws  IndexOutOfBoundsException {@inheritDoc}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     */
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
   164
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    public int read(char cbuf[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            int n = super.read(cbuf, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            for (int i = off; i < off + n; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                int c = cbuf[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                if (skipLF) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                    skipLF = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    if (c == '\n')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    skipLF = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                case '\n':      /* Fall through */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
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
     * Read a line of text.  Whenever a <a href="#lt">line terminator</a> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * read the current line number is incremented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return  A String containing the contents of the line, not including
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *          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
   195
     *          {@code null} if the end of the stream has been reached
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *          If an I/O error occurs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    public String readLine() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        synchronized (lock) {
58089
e64fec9f1773 8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents: 54643
diff changeset
   202
            boolean[] term = new boolean[1];
e64fec9f1773 8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents: 54643
diff changeset
   203
            String l = super.readLine(skipLF, term);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            skipLF = false;
58089
e64fec9f1773 8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents: 54643
diff changeset
   205
            if (l != null && term[0])
2
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) {
54643
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
   264
            // If the most recently read character is '\r', then increment the
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
   265
            // read ahead limit as in this case if the next character is '\n',
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
   266
            // two characters would actually be read by the next read().
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
   267
            if (skipLF)
3edf22a7cbaf 8218280: LineNumberReader throws "Mark invalid" exception if CRLF straddles buffer.
bpb
parents: 47216
diff changeset
   268
                readAheadLimit++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            super.mark(readAheadLimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            markedLineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            markedSkipLF     = skipLF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Reset the stream to the most recent mark.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * @throws  IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *          If the stream has not been marked, or if the mark has been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *          invalidated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            super.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            lineNumber = markedLineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            skipLF     = markedSkipLF;
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
}