src/java.base/share/classes/java/io/InputStream.java
author bpb
Wed, 22 Nov 2017 08:12:45 -0800
changeset 47920 52c9e8d2f8d9
parent 47216 71c04702a3d5
child 48366 2c1af559e922
permissions -rw-r--r--
8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks Summary: Reduce parameter bounds checks from five to three as in InputStream::read Reviewed-by: psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
47920
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2017, 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
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
    28
import java.util.Arrays;
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
    29
import java.util.Objects;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
    30
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * This abstract class is the superclass of all classes representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * an input stream of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p> Applications that need to define a subclass of <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * must always provide a method that returns the next byte of input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @see     java.io.BufferedInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @see     java.io.ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.io.DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see     java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see     java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see     java.io.PushbackInputStream
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 21334
diff changeset
    46
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class InputStream implements Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    50
    // 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
    51
    // use when skipping.
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    52
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
    54
    private static final int DEFAULT_BUFFER_SIZE = 8192;
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
    55
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * Reads the next byte of data from the input stream. The value byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * returned as an <code>int</code> in the range <code>0</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * <code>255</code>. If no byte is available because the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * has been reached, the value <code>-1</code> is returned. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * blocks until input data is available, the end of the stream is detected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * <p> A subclass must provide an implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *             stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    public abstract int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * Reads some number of bytes from the input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * returned as an integer.  This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * available, end of file is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * least one byte. If no byte is available because the stream is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * end of the file, the value <code>-1</code> is returned; otherwise, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * least one byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * <p> The first byte read is stored into element <code>b[0]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * number of bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <code>b[b.length-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @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
    97
     *             <code>-1</code> if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @exception  IOException  If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * other than the end of the file, if the input stream has been closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Reads up to <code>len</code> bytes of data from the input stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * an array of bytes.  An attempt is made to read as many as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * <code>len</code> bytes, but a smaller number may be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * The number of bytes actually read is returned as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <p> This method blocks until input data is available, end of file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * <p> If <code>len</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * least one byte. If no byte is available because the stream is at end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * file, the value <code>-1</code> is returned; otherwise, at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * <p> The first byte read is stored into element <code>b[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * <code>b[off+len-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * <p> In every case, elements <code>b[0]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <code>b[off]</code> and elements <code>b[off+len]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * <code>b[b.length-1]</code> are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * for class <code>InputStream</code> simply calls the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * <code>read()</code> repeatedly. If the first such call results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <code>IOException</code>, that exception is returned from the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * any subsequent call to <code>read()</code> results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <code>IOException</code>, the exception is caught and treated as if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * were end of file; the bytes read up to that point are stored into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * <code>b</code> and the number of bytes read before the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * occurred is returned. The default implementation of this method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * until the requested amount of input data <code>len</code> has been read,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * end of file is detected, or an exception is thrown. Subclasses are encouraged
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * @param      off   the start offset in array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param      len   the maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * other than end of file, or if the input stream has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    public int read(byte b[], int off, int len) throws IOException {
47920
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   167
        Objects.requireNonNull(b);
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   168
        Objects.checkFromIndexSize(off, len, b.length);
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   169
        if (len == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   194
     * The maximum size of array to allocate.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   195
     * Some VMs reserve some header words in an array.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   196
     * Attempts to allocate larger arrays may result in
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   197
     * OutOfMemoryError: Requested array size exceeds VM limit
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   198
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   199
    private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   200
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   201
    /**
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   202
     * Reads all remaining bytes from the input stream. This method blocks until
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   203
     * all remaining bytes have been read and end of stream is detected, or an
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   204
     * exception is thrown. This method does not close the input stream.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   205
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   206
     * <p> When this stream reaches end of stream, further invocations of this
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   207
     * method will return an empty byte array.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   208
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   209
     * <p> Note that this method is intended for simple cases where it is
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   210
     * convenient to read all bytes into a byte array. It is not intended for
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   211
     * reading input streams with large amounts of data.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   212
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   213
     * <p> The behavior for the case where the input stream is <i>asynchronously
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   214
     * closed</i>, or the thread interrupted during the read, is highly input
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   215
     * stream specific, and therefore not specified.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   216
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   217
     * <p> If an I/O error occurs reading from the input stream, then it may do
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   218
     * so after some, but not all, bytes have been read. Consequently the input
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   219
     * stream may not be at end of stream and may be in an inconsistent state.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   220
     * It is strongly recommended that the stream be promptly closed if an I/O
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   221
     * error occurs.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   222
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   223
     * @return a byte array containing the bytes read from this input stream
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   224
     * @throws IOException if an I/O error occurs
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   225
     * @throws OutOfMemoryError if an array of the required size cannot be
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   226
     *         allocated. For example, if an array larger than {@code 2GB} would
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   227
     *         be required to store the bytes.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   228
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   229
     * @since 9
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   230
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   231
    public byte[] readAllBytes() throws IOException {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   232
        byte[] buf = new byte[DEFAULT_BUFFER_SIZE];
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   233
        int capacity = buf.length;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   234
        int nread = 0;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   235
        int n;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   236
        for (;;) {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   237
            // read to EOF which may read more or less than initial buffer size
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   238
            while ((n = read(buf, nread, capacity - nread)) > 0)
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   239
                nread += n;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   240
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   241
            // if the last call to read returned -1, then we're done
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   242
            if (n < 0)
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   243
                break;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   244
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   245
            // need to allocate a larger buffer
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   246
            if (capacity <= MAX_BUFFER_SIZE - capacity) {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   247
                capacity = capacity << 1;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   248
            } else {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   249
                if (capacity == MAX_BUFFER_SIZE)
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   250
                    throw new OutOfMemoryError("Required array size too large");
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   251
                capacity = MAX_BUFFER_SIZE;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   252
            }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   253
            buf = Arrays.copyOf(buf, capacity);
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   254
        }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   255
        return (capacity == nread) ? buf : Arrays.copyOf(buf, nread);
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   256
    }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   257
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   258
    /**
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   259
     * Reads the requested number of bytes from the input stream into the given
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   260
     * byte array. This method blocks until {@code len} bytes of input data have
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   261
     * been read, end of stream is detected, or an exception is thrown. The
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   262
     * number of bytes actually read, possibly zero, is returned. This method
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   263
     * does not close the input stream.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   264
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   265
     * <p> In the case where end of stream is reached before {@code len} bytes
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   266
     * have been read, then the actual number of bytes read will be returned.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   267
     * When this stream reaches end of stream, further invocations of this
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   268
     * method will return zero.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   269
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   270
     * <p> If {@code len} is zero, then no bytes are read and {@code 0} is
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   271
     * returned; otherwise, there is an attempt to read up to {@code len} bytes.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   272
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   273
     * <p> The first byte read is stored into element {@code b[off]}, the next
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   274
     * one in to {@code b[off+1]}, and so on. The number of bytes read is, at
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   275
     * most, equal to {@code len}. Let <i>k</i> be the number of bytes actually
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   276
     * read; these bytes will be stored in elements {@code b[off]} through
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   277
     * {@code b[off+}<i>k</i>{@code -1]}, leaving elements {@code b[off+}<i>k</i>
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   278
     * {@code ]} through {@code b[off+len-1]} unaffected.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   279
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   280
     * <p> The behavior for the case where the input stream is <i>asynchronously
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   281
     * closed</i>, or the thread interrupted during the read, is highly input
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   282
     * stream specific, and therefore not specified.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   283
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   284
     * <p> If an I/O error occurs reading from the input stream, then it may do
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   285
     * so after some, but not all, bytes of {@code b} have been updated with
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   286
     * data from the input stream. Consequently the input stream and {@code b}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   287
     * may be in an inconsistent state. It is strongly recommended that the
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   288
     * stream be promptly closed if an I/O error occurs.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   289
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   290
     * @param  b the byte array into which the data is read
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   291
     * @param  off the start offset in {@code b} at which the data is written
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   292
     * @param  len the maximum number of bytes to read
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   293
     * @return the actual number of bytes read into the buffer
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   294
     * @throws IOException if an I/O error occurs
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   295
     * @throws NullPointerException if {@code b} is {@code null}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   296
     * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   297
     *         is negative, or {@code len} is greater than {@code b.length - off}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   298
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   299
     * @since 9
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   300
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   301
    public int readNBytes(byte[] b, int off, int len) throws IOException {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   302
        Objects.requireNonNull(b);
47920
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   303
        Objects.checkFromIndexSize(off, len, b.length);
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   304
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   305
        int n = 0;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   306
        while (n < len) {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   307
            int count = read(b, off + n, len - n);
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   308
            if (count < 0)
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   309
                break;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   310
            n += count;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   311
        }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   312
        return n;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   313
    }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   314
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   315
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * Skips over and discards <code>n</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * stream. The <code>skip</code> method may, for a variety of reasons, end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * up skipping over some smaller number of bytes, possibly <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * This may result from any of a number of conditions; reaching end of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * 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
   321
     * The actual number of bytes skipped is returned. If {@code n} is
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   322
     * negative, the {@code skip} method for class {@code InputStream} always
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   323
     * returns 0, and no bytes are skipped. Subclasses may handle the negative
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   324
     * value differently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
39121
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 35302
diff changeset
   326
     * <p> The <code>skip</code> method implementation of this class creates a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * byte array and then repeatedly reads into it until <code>n</code> bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * have been read or the end of the stream has been reached. Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * encouraged to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * For instance, the implementation may depend on the ability to seek.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return     the actual number of bytes skipped.
39121
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 35302
diff changeset
   334
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        long remaining = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   345
        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
   346
        byte[] skipBuffer = new byte[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        while (remaining > 0) {
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   348
            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (nr < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
            remaining -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        return n - remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * invocation of a method for this input stream. The next invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * might be the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * <p> Note that while some implementations of {@code InputStream} will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * the total number of bytes in the stream, many will not.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * never correct to use the return value of this method to allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * a buffer intended to hold all data in this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p> A subclass' implementation of this method may choose to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * {@link IOException} if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * invoking the {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <p> The {@code available} method for class {@code InputStream} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * returns {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * <p> This method should be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return     an estimate of the number of bytes that can be read (or skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *             over) from this input stream without blocking or {@code 0} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     *             it reaches the end of the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * @exception  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * <p> The <code>close</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    public void close() throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * Marks the current position in this input stream. A subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * the <code>reset</code> method repositions this stream at the last marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * position so that subsequent reads re-read the same bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * <p> The <code>readlimit</code> arguments tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * <p> The general contract of <code>mark</code> is that, if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <code>markSupported</code> returns <code>true</code>, the stream somehow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * remembers all the bytes read after the call to <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * stands ready to supply those same bytes again if and whenever the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <code>reset</code> is called.  However, the stream is not required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * remember any data at all if more than <code>readlimit</code> bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * read from the stream before <code>reset</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <p> Marking a closed stream should not have any effect on the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p> The <code>mark</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public synchronized void mark(int readlimit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p> The general contract of <code>reset</code> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 17691
diff changeset
   433
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <code>true</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *     <ul><li> If the method <code>mark</code> has not been called since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     *     the stream was created, or the number of bytes read from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *     since <code>mark</code> was last called is larger than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     *     to <code>mark</code> at that last call, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *     <code>IOException</code> might be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *     <li> If such an <code>IOException</code> is not thrown, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *     stream is reset to a state such that all the bytes read since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     *     most recent call to <code>mark</code> (or since the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *     file, if <code>mark</code> has not been called) will be resupplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *     to subsequent callers of the <code>read</code> method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     *     any bytes that otherwise would have been the next input data as of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *     the time of the call to <code>reset</code>. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * <code>false</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *     <ul><li> The call to <code>reset</code> may throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *     <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *     <li> If an <code>IOException</code> is not thrown, then the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     *     is reset to a fixed state that depends on the particular type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *     input stream and how it was created. The bytes that will be supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *     to subsequent callers of the <code>read</code> method depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *     particular type of the input stream. </ul></ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * <p>The method <code>reset</code> for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * does nothing except throw an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @exception  IOException  if this stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * <code>reset</code> methods. Whether or not <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <code>reset</code> are supported is an invariant property of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * particular input stream instance. The <code>markSupported</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * of <code>InputStream</code> returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @return  <code>true</code> if this stream instance supports the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     *          and reset methods; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   491
    /**
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   492
     * Reads all bytes from this input stream and writes the bytes to the
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   493
     * given output stream in the order that they are read. On return, this
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   494
     * input stream will be at end of stream. This method does not close either
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   495
     * stream.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   496
     * <p>
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   497
     * This method may block indefinitely reading from the input stream, or
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   498
     * writing to the output stream. The behavior for the case where the input
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   499
     * and/or output stream is <i>asynchronously closed</i>, or the thread
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   500
     * interrupted during the transfer, is highly input and output stream
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   501
     * specific, and therefore not specified.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   502
     * <p>
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   503
     * If an I/O error occurs reading from the input stream or writing to the
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   504
     * output stream, then it may do so after some bytes have been read or
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   505
     * written. Consequently the input stream may not be at end of stream and
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   506
     * one, or both, streams may be in an inconsistent state. It is strongly
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   507
     * recommended that both streams be promptly closed if an I/O error occurs.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   508
     *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   509
     * @param  out the output stream, non-null
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   510
     * @return the number of bytes transferred
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   511
     * @throws IOException if an I/O error occurs when reading or writing
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   512
     * @throws NullPointerException if {@code out} is {@code null}
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   513
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   514
     * @since 9
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   515
     */
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   516
    public long transferTo(OutputStream out) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   517
        Objects.requireNonNull(out, "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   518
        long transferred = 0;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   519
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   520
        int read;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   521
        while ((read = this.read(buffer, 0, DEFAULT_BUFFER_SIZE)) >= 0) {
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   522
            out.write(buffer, 0, read);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   523
            transferred += read;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   524
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   525
        return transferred;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   526
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
}