src/java.base/share/classes/java/io/PushbackInputStream.java
author chegar
Thu, 17 Oct 2019 20:54:25 +0100
branchdatagramsocketimpl-branch
changeset 58679 9c3209ff7550
parent 58678 9cf78a70fa4f
parent 58288 48e480e56aad
permissions -rw-r--r--
datagramsocketimpl-branch: merge with default
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
55730
94f8a0b34117 8073213: javadoc of PushbackInputStream methods should specify NullPointerExceptions
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 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
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    29
 * A {@code PushbackInputStream} adds
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * functionality to another input stream, namely
28867
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    31
 * the  ability to "push back" or "unread" bytes,
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    32
 * by storing pushed-back bytes in an internal buffer.
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    33
 * This is useful in situations where
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    34
 * it is convenient for a fragment of code
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * to read an indefinite number of data bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * that  are delimited by a particular byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * value; after reading the terminating byte,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * the  code fragment can "unread" it, so that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * the next read operation on the input stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * will reread the byte that was pushed back.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * For example, bytes representing the  characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * constituting an identifier might be terminated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * by a byte representing an  operator character;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * a method whose job is to read just an identifier
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * can read until it  sees the operator and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * then push the operator back to be re-read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author  David Connelly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @author  Jonathan Payne
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    50
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
class PushbackInputStream extends FilterInputStream {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The pushback buffer.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    56
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected byte[] buf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * The position within the pushback buffer from which the next byte will
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    62
     * be read.  When the buffer is empty, {@code pos} is equal to
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    63
     * {@code buf.length}; when the buffer is full, {@code pos} is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * equal to zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    66
     * @since   1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    protected int pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * Check to make sure that this stream has not been closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private void ensureOpen() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            throw new IOException("Stream closed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    79
     * Creates a {@code PushbackInputStream}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    80
     * with a pushback buffer of the specified {@code size},
28867
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    81
     * and saves its argument, the input stream
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    82
     * {@code in}, for later use. Initially,
28867
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
    83
     * the pushback buffer is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @param  in    the input stream from which bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @param  size  the size of the pushback buffer.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
    87
     * @throws IllegalArgumentException if {@code size <= 0}
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
    88
     * @since  1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    public PushbackInputStream(InputStream in, int size) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        super(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        if (size <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            throw new IllegalArgumentException("size <= 0");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        this.buf = new byte[size];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.pos = size;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   100
     * Creates a {@code PushbackInputStream}
28867
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
   101
     * with a 1-byte pushback buffer, and saves its argument, the input stream
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   102
     * {@code in}, for later use. Initially,
28867
470553603264 8064562: (doc) errors in java.io.PushbackInputStream API documentation
bpb
parents: 25859
diff changeset
   103
     * the pushback buffer is empty.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * @param   in   the input stream from which bytes will be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public PushbackInputStream(InputStream in) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        this(in, 1);
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
     * Reads the next byte of data from this input stream. The value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   113
     * byte is returned as an {@code int} in the range
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   114
     * {@code 0} to {@code 255}. If no byte is available
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * because the end of the stream has been reached, the value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   116
     * {@code -1} is returned. This method blocks until input data
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * is available, the end of the stream is detected, or an exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * <p> This method returns the most recently pushed-back byte, if there is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   121
     * one, and otherwise calls the {@code read} method of its underlying
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * input stream and returns whatever value that method returns.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   124
     * @return     the next byte of data, or {@code -1} if the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *             stream has been reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   126
     * @throws     IOException  if this input stream has been closed by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    public int read() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (pos < buf.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            return buf[pos++] & 0xff;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return super.read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   140
     * Reads up to {@code len} bytes of data from this input stream into
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * an array of bytes.  This method first reads any pushed-back bytes; after
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   142
     * that, if fewer than {@code len} bytes have been read then it
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   143
     * reads from the underlying input stream. If {@code len} is not zero, the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * blocks until at least 1 byte of input is available; otherwise, no
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   145
     * bytes are read and {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * @param      b     the buffer into which the data is read.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   148
     * @param      off   the start offset in the destination array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @return     the total number of bytes read into the buffer, or
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   151
     *             {@code -1} if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *             the stream has been reached.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   153
     * @throws     NullPointerException If {@code b} is {@code null}.
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   154
     * @throws     IndexOutOfBoundsException If {@code off} is negative,
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   155
     *             {@code len} is negative, or {@code len} is greater than
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   156
     *             {@code b.length - off}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   157
     * @throws     IOException  if this input stream has been closed by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    public int read(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        int avail = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        if (avail > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (len < avail) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                avail = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            System.arraycopy(buf, pos, b, off, avail);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            pos += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            off += avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            len -= avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
            len = super.read(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (len == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                return avail == 0 ? -1 : avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            return avail + len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        return avail;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Pushes back a byte by copying it to the front of the pushback buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * After this method returns, the next byte to be read will have the value
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   195
     * {@code (byte)b}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   197
     * @param      b   the {@code int} value whose low-order
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     *                  byte is to be pushed back.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   199
     * @throws    IOException If there is not enough room in the pushback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *            buffer for the byte, or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *            invoking its {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public void unread(int b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (pos == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            throw new IOException("Push back buffer is full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        buf[--pos] = (byte)b;
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
     * Pushes back a portion of an array of bytes by copying it to the front
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * of the pushback buffer.  After this method returns, the next byte to be
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   214
     * read will have the value {@code b[off]}, the byte after that will
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   215
     * have the value {@code b[off+1]}, and so forth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   217
     * @param     b the byte array to push back.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   218
     * @param     off the start offset of the data.
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   219
     * @param     len the number of bytes to push back.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   220
     * @throws    NullPointerException If {@code b} is {@code null}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   221
     * @throws    IOException If there is not enough room in the pushback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *            buffer for the specified number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     *            or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *            invoking its {@link #close()} method.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   225
     * @since     1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
    public void unread(byte[] b, int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        if (len > pos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            throw new IOException("Push back buffer is full");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        pos -= len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        System.arraycopy(b, off, buf, pos, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * Pushes back an array of bytes by copying it to the front of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * pushback buffer.  After this method returns, the next byte to be read
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   239
     * will have the value {@code b[0]}, the byte after that will have the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   240
     * value {@code b[1]}, and so forth.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   242
     * @param     b the byte array to push back
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   243
     * @throws    NullPointerException If {@code b} is {@code null}.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   244
     * @throws    IOException If there is not enough room in the pushback
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *            buffer for the specified number of bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *            or this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *            invoking its {@link #close()} method.
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23010
diff changeset
   248
     * @since     1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    public void unread(byte[] b) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        unread(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * invocation of a method for this input stream. The next invocation might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <p> The method returns the sum of the number of bytes that have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * pushed back and the value returned by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * java.io.FilterInputStream#available available}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @return     the number of bytes that can be read (or skipped over) from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *             the input stream without blocking.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   267
     * @throws     IOException  if this input stream has been closed by
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *             invoking its {@link #close()} method,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     *             or an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @see        java.io.InputStream#available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        ensureOpen();
7280
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   275
        int n = buf.length - pos;
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   276
        int avail = super.available();
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   277
        return n > (Integer.MAX_VALUE - avail)
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   278
                    ? Integer.MAX_VALUE
81f10887bf74 6631046: BufferedInputStream.available() reports negative int on very large inputstream
mchung
parents: 5506
diff changeset
   279
                    : n + avail;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   283
     * Skips over and discards {@code n} bytes of data from this
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   284
     * input stream. The {@code skip} method may, for a variety of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * reasons, end up skipping over some smaller number of bytes,
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   286
     * possibly zero.  If {@code n} is negative, no bytes are skipped.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   288
     * <p> The {@code skip} method of {@code PushbackInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * first skips over the bytes in the pushback buffer, if any.  It then
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   290
     * calls the {@code skip} method of the underlying input stream if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * more bytes need to be skipped.  The actual number of bytes skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * @param      n  {@inheritDoc}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @return     {@inheritDoc}
39121
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 28867
diff changeset
   296
     * @throws     IOException  if the stream has been closed by
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 28867
diff changeset
   297
     *             invoking its {@link #close()} method,
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 28867
diff changeset
   298
     *             {@code in.skip(n)} throws an IOException,
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 28867
diff changeset
   299
     *             or an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @see        java.io.FilterInputStream#in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @see        java.io.InputStream#skip(long n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @since      1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        ensureOpen();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        long pskip = buf.length - pos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        if (pskip > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            if (n < pskip) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                pskip = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            pos += pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            n -= pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        if (n > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            pskip += super.skip(n);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return pskip;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   325
     * Tests if this input stream supports the {@code mark} and
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   326
     * {@code reset} methods, which it does not.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   328
     * @return   {@code false}, since this class does not support the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   329
     *           {@code mark} and {@code reset} methods.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   330
     * @see      java.io.InputStream#mark(int)
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   331
     * @see      java.io.InputStream#reset()
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Marks the current position in this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   340
     * <p> The {@code mark} method of {@code PushbackInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public synchronized void mark(int readlimit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Repositions this stream to the position at the time the
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   352
     * {@code mark} method was last called on this input stream.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   354
     * <p> The method {@code reset} for class
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   355
     * {@code PushbackInputStream} does nothing except throw an
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   356
     * {@code IOException}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   358
     * @throws  IOException  if this method is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * associated with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * Once the stream has been closed, further read(), unread(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * available(), reset(), or skip() invocations will throw an IOException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * Closing a previously closed stream has no effect.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 55730
diff changeset
   373
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    public synchronized void close() throws IOException {
8540
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   376
        if (in == null)
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   377
            return;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   378
        in.close();
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   379
        in = null;
ed028ce13912 7021327: Changes for 7020888 included changes to other files in error
alanb
parents: 8539
diff changeset
   380
        buf = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
}