jdk/src/share/classes/java/io/InputStream.java
author rriggs
Tue, 22 Oct 2013 17:02:08 -0400
changeset 21334 c60dfce46a77
parent 17691 13931cd9405f
child 24865 09b1d992ca72
permissions -rw-r--r--
8026982: javadoc errors in core libs Summary: Cleanup of javadoc -Xlint errors Reviewed-by: lancea, mduigou, darcy, mullan, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
     2
 * Copyright (c) 1994, 2013, 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: 1942
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: 1942
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: 1942
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1942
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1942
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 abstract class is the superclass of all classes representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * an input stream of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * <p> Applications that need to define a subclass of <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * must always provide a method that returns the next byte of input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @see     java.io.BufferedInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @see     java.io.ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see     java.io.DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     java.io.PushbackInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since   JDK1.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public abstract class InputStream implements Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    47
    // MAX_SKIP_BUFFER_SIZE is used to determine the maximum buffer size to
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    48
    // use when skipping.
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    49
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Reads the next byte of data from the input stream. The value byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * returned as an <code>int</code> in the range <code>0</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * <code>255</code>. If no byte is available because the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * has been reached, the value <code>-1</code> is returned. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * blocks until input data is available, the end of the stream is detected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <p> A subclass must provide an implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *             stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public abstract int read() throws IOException;
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 some number of bytes from the input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * returned as an integer.  This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * available, end of file is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * least one byte. If no byte is available because the stream is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * end of the file, the value <code>-1</code> is returned; otherwise, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * least one byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <p> The first byte read is stored into element <code>b[0]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * number of bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * <code>b[b.length-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @return     the total number of bytes read into the buffer, or
1942
bdfd8f01987a 6799462: Minor typo (wrong word) in JavaDoc for InputStream.read(byte[] b) method
darcy
parents: 2
diff changeset
    92
     *             <code>-1</code> if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @exception  IOException  If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * other than the end of the file, if the input stream has been closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Reads up to <code>len</code> bytes of data from the input stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * an array of bytes.  An attempt is made to read as many as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <code>len</code> bytes, but a smaller number may be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * The number of bytes actually read is returned as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p> This method blocks until input data is available, end of file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p> If <code>len</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * least one byte. If no byte is available because the stream is at end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * file, the value <code>-1</code> is returned; otherwise, at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <p> The first byte read is stored into element <code>b[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * <code>b[off+len-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> In every case, elements <code>b[0]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>b[off]</code> and elements <code>b[off+len]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * <code>b[b.length-1]</code> are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * for class <code>InputStream</code> simply calls the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>read()</code> repeatedly. If the first such call results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>IOException</code>, that exception is returned from the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * any subsequent call to <code>read()</code> results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * <code>IOException</code>, the exception is caught and treated as if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * were end of file; the bytes read up to that point are stored into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>b</code> and the number of bytes read before the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * occurred is returned. The default implementation of this method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * until the requested amount of input data <code>len</code> has been read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * end of file is detected, or an exception is thrown. Subclasses are encouraged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @param      off   the start offset in array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * @param      len   the maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * other than end of file, or if the input stream has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                if (c == -1) {
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
                b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * Skips over and discards <code>n</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * stream. The <code>skip</code> method may, for a variety of reasons, end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * up skipping over some smaller number of bytes, possibly <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * This may result from any of a number of conditions; reaching end of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * before <code>n</code> bytes have been skipped is only one possibility.
17691
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   196
     * The actual number of bytes skipped is returned. If {@code n} is
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   197
     * negative, the {@code skip} method for class {@code InputStream} always
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   198
     * returns 0, and no bytes are skipped. Subclasses may handle the negative
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   199
     * value differently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * <p> The <code>skip</code> method of this class creates a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * byte array and then repeatedly reads into it until <code>n</code> bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * have been read or the end of the stream has been reached. Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * encouraged to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * For instance, the implementation may depend on the ability to seek.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @return     the actual number of bytes skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * @exception  IOException  if the stream does not support seek,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *                          or if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        long remaining = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   221
        int size = (int)Math.min(MAX_SKIP_BUFFER_SIZE, remaining);
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   222
        byte[] skipBuffer = new byte[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        while (remaining > 0) {
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   224
            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            if (nr < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            remaining -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        return n - remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * invocation of a method for this input stream. The next invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * might be the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <p> Note that while some implementations of {@code InputStream} will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * the total number of bytes in the stream, many will not.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * never correct to use the return value of this method to allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * a buffer intended to hold all data in this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * <p> A subclass' implementation of this method may choose to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * {@link IOException} if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * invoking the {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * <p> The {@code available} method for class {@code InputStream} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * returns {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * <p> This method should be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     * @return     an estimate of the number of bytes that can be read (or skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     *             over) from this input stream without blocking or {@code 0} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *             it reaches the end of the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @exception  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     * <p> The <code>close</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    public void close() throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * Marks the current position in this input stream. A subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * the <code>reset</code> method repositions this stream at the last marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * position so that subsequent reads re-read the same bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * <p> The <code>readlimit</code> arguments tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <p> The general contract of <code>mark</code> is that, if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * <code>markSupported</code> returns <code>true</code>, the stream somehow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * remembers all the bytes read after the call to <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * stands ready to supply those same bytes again if and whenever the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * <code>reset</code> is called.  However, the stream is not required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * remember any data at all if more than <code>readlimit</code> bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * read from the stream before <code>reset</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <p> Marking a closed stream should not have any effect on the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * <p> The <code>mark</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    public synchronized void mark(int readlimit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * <p> The general contract of <code>reset</code> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 17691
diff changeset
   309
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <code>true</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *     <ul><li> If the method <code>mark</code> has not been called since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     *     the stream was created, or the number of bytes read from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *     since <code>mark</code> was last called is larger than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *     to <code>mark</code> at that last call, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *     <code>IOException</code> might be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     *     <li> If such an <code>IOException</code> is not thrown, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     *     stream is reset to a state such that all the bytes read since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *     most recent call to <code>mark</code> (or since the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     *     file, if <code>mark</code> has not been called) will be resupplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     *     to subsequent callers of the <code>read</code> method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *     any bytes that otherwise would have been the next input data as of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *     the time of the call to <code>reset</code>. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * <code>false</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *     <ul><li> The call to <code>reset</code> may throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *     <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *     <li> If an <code>IOException</code> is not thrown, then the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *     is reset to a fixed state that depends on the particular type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *     input stream and how it was created. The bytes that will be supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *     to subsequent callers of the <code>read</code> method depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *     particular type of the input stream. </ul></ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <p>The method <code>reset</code> for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * does nothing except throw an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception  IOException  if this stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * <code>reset</code> methods. Whether or not <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * <code>reset</code> are supported is an invariant property of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * particular input stream instance. The <code>markSupported</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * of <code>InputStream</code> returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * @return  <code>true</code> if this stream instance supports the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     *          and reset methods; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
}