src/java.base/share/classes/java/io/LineNumberInputStream.java
author jboes
Fri, 20 Sep 2019 11:07:52 +0100
changeset 58242 94bb65cb37d3
parent 47216 71c04702a3d5
child 59201 b24f4caa1411
permissions -rw-r--r--
8230648: Replace @exception tag with @throws in java.base Summary: Minor coding style update of javadoc tag in any file in java.base Reviewed-by: prappo, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
     2
 * Copyright (c) 1995, 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
 * This class is an input stream filter that provides the added
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * functionality of keeping track of the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * A line is a sequence of bytes ending with a carriage return
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    33
 * character ({@code '\u005Cr'}), a newline character
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    34
 * ({@code '\u005Cn'}), or a carriage return character followed
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * immediately by a linefeed character. In all three cases, the line
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * terminating character(s) are returned as a single newline character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    38
 * The line number begins at {@code 0}, and is incremented by
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    39
 * {@code 1} when a {@code read} returns a newline character.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @author     Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see        java.io.LineNumberReader
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 11676
diff changeset
    43
 * @since      1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @deprecated This class incorrectly assumes that bytes adequately represent
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *             characters.  As of JDK&nbsp;1.1, the preferred way to operate on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *             character streams is via the new character-stream classes, which
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *             include a class for counting line numbers.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
@Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
class LineNumberInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    int pushBack = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    int lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    int markLineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    int markPushBack = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * Constructs a newline number input stream that reads its input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * from the specified input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @param      in   the underlying input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public LineNumberInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Reads the next byte of data from this input stream. The value
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    69
     * byte is returned as an {@code int} in the range
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    70
     * {@code 0} to {@code 255}. If no byte is available
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * because the end of the stream has been reached, the value
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    72
     * {@code -1} is returned. This method blocks until input data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * is available, the end of the stream is detected, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    76
     * The {@code read} method of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    77
     * {@code LineNumberInputStream} calls the {@code read}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * method of the underlying input stream. It checks for carriage
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * returns and newline characters in the input, and modifies the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * current line number as appropriate. A carriage-return character or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * a carriage return followed by a newline character are both
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * converted into a single newline character.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
    84
     * @return     the next byte of data, or {@code -1} if the end of this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *             stream is reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
    86
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * @see        java.io.LineNumberInputStream#getLineNumber()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
11121
4cdbb7f9480f 7116890: additional warnings fixes for java.io
smarks
parents: 5506
diff changeset
    90
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int c = pushBack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (c != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            pushBack = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            c = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        switch (c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
          case '\r':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            pushBack = in.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (pushBack == '\n') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                pushBack = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
          case '\n':
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            lineNumber++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            return '\n';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   114
     * Reads up to {@code len} bytes of data from this input stream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * into an array of bytes. This method blocks until some input is available.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   117
     * The {@code read} method of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   118
     * {@code LineNumberInputStream} repeatedly calls the
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   119
     * {@code read} method of zero arguments to fill in the byte array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param      off   the start offset of the data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @return     the total number of bytes read into the buffer, or
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   125
     *             {@code -1} if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *             this stream has been reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   127
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * @see        java.io.LineNumberInputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        } else if ((off < 0) || (off > b.length) || (len < 0) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                   ((off + len) > b.length) || ((off + len) < 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                if (b != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   163
     * Skips over and discards {@code n} bytes of data from this
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   164
     * input stream. The {@code skip} method may, for a variety of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * reasons, end up skipping over some smaller number of bytes,
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   166
     * possibly {@code 0}. The actual number of bytes skipped is
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   167
     * returned.  If {@code n} is negative, no bytes are skipped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   169
     * The {@code skip} method of {@code LineNumberInputStream} creates
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * a byte array and then repeatedly reads into it until
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   171
     * {@code n} bytes have been read or the end of the stream has
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return     the actual number of bytes skipped.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   176
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int chunk = 2048;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        long remaining = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        byte data[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        data = new byte[chunk];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        while (remaining > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            nr = read(data, 0, (int) Math.min(chunk, remaining));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            if (nr < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            remaining -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return n - remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * Sets the line number to the specified argument.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * @param      lineNumber   the new line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @see #getLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    public void setLineNumber(int lineNumber) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        this.lineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * @return     the current line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @see #setLineNumber
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    public int getLineNumber() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        return lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * Returns the number of bytes that can be read from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * stream without blocking.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * Note that if the underlying input stream is able to supply
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <i>k</i> input characters without blocking, the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   228
     * {@code LineNumberInputStream} can guarantee only to provide
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <i>k</i>/2 characters without blocking, because the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <i>k</i> characters from the underlying input stream might
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   231
     * consist of <i>k</i>/2 pairs of {@code '\u005Cr'} and
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   232
     * {@code '\u005Cn'}, which are converted to just
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   233
     * <i>k</i>/2 {@code '\u005Cn'} characters.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @return     the number of bytes that can be read from this input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *             without blocking.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   237
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        return (pushBack == -1) ? super.available()/2 : super.available()/2 + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Marks the current position in this input stream. A subsequent
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   246
     * call to the {@code reset} method repositions this stream at
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * the last marked position so that subsequent reads re-read the same bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   249
     * The {@code mark} method of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   250
     * {@code LineNumberInputStream} remembers the current line
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   251
     * number in a private variable, and then calls the {@code mark}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * method of the underlying input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @see     java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * @see     java.io.LineNumberInputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    public void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        markLineNumber = lineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        markPushBack   = pushBack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        in.mark(readlimit);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * Repositions this stream to the position at the time the
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   267
     * {@code mark} method was last called on this input stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p>
11676
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   269
     * The {@code reset} method of
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   270
     * {@code LineNumberInputStream} resets the line number to be
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   271
     * the line number at the time the {@code mark} method was
7e75ec031b97 7132338: Use @code friendly idiom for '\' in javadoc
darcy
parents: 11121
diff changeset
   272
     * called, and then calls the {@code reset} method of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * underlying input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * Stream marks are intended to be used in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * situations where you need to read ahead a little to see what's in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the stream. Often this is most easily done by invoking some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * general parser. If the stream is of the type handled by the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * parser, it just chugs along happily. If the stream is not of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * that type, the parser should toss an exception when it fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * which, if it happens within readlimit bytes, allows the outer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * code to reset the stream and try another parser.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 47216
diff changeset
   284
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * @see        java.io.LineNumberInputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    public void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        lineNumber = markLineNumber;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        pushBack   = markPushBack;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        in.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
}