src/java.base/share/classes/java/io/InputStream.java
author bpb
Thu, 01 Feb 2018 09:40:38 -0800
changeset 48696 917868f73209
parent 48461 6a1c3a5e04f3
child 49238 63eceefeb347
permissions -rw-r--r--
8139206: Add InputStream readNBytes(int len) Reviewed-by: alanb, chegar, plevart, apetcher, rriggs, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
     2
 * Copyright (c) 1994, 2018, 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
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
    28
import java.util.ArrayList;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
    29
import java.util.Arrays;
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
    30
import java.util.List;
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
    31
import java.util.Objects;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
    32
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * This abstract class is the superclass of all classes representing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * an input stream of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * <p> Applications that need to define a subclass of <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * must always provide a method that returns the next byte of input.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author  Arthur van Hoff
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * @see     java.io.BufferedInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @see     java.io.ByteArrayInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @see     java.io.DataInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @see     java.io.FilterInputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see     java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see     java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @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
    48
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
public abstract class InputStream implements Closeable {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    52
    // 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
    53
    // use when skipping.
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
    54
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
    56
    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
    57
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /**
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    59
     * Returns a new {@code InputStream} that reads no bytes. The returned
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    60
     * stream is initially open.  The stream is closed by calling the
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    61
     * {@code close()} method.  Subsequent calls to {@code close()} have no
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    62
     * effect.
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    63
     *
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    64
     * <p> While the stream is open, the {@code available()}, {@code read()},
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    65
     * {@code read(byte[])}, {@code read(byte[], int, int)},
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
    66
     * {@code readAllBytes()}, {@code readNBytes(byte[], int, int)},
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
    67
     * {@code readNBytes(int)}, {@code skip(long)}, and
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    68
     * {@code transferTo()} methods all behave as if end of stream has been
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    69
     * reached.  After the stream has been closed, these methods all throw
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    70
     * {@code IOException}.
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    71
     *
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    72
     * <p> The {@code markSupported()} method returns {@code false}.  The
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    73
     * {@code mark()} method does nothing, and the {@code reset()} method
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    74
     * throws {@code IOException}.
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    75
     *
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    76
     * @return an {@code InputStream} which contains no bytes
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    77
     *
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    78
     * @since 11
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    79
     */
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    80
    public static InputStream nullInputStream() {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    81
        return new InputStream() {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    82
            private volatile boolean closed;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    83
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    84
            private void ensureOpen() throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    85
                if (closed) {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    86
                    throw new IOException("Stream closed");
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    87
                }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    88
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    89
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    90
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    91
            public int available () throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    92
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    93
                return 0;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    94
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    95
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    96
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    97
            public int read() throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    98
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
    99
                return -1;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   100
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   101
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   102
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   103
            public int read(byte[] b, int off, int len) throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   104
                Objects.checkFromIndexSize(off, len, b.length);
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   105
                if (len == 0) {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   106
                    return 0;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   107
                }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   108
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   109
                return -1;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   110
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   111
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   112
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   113
            public byte[] readAllBytes() throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   114
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   115
                return new byte[0];
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   116
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   117
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   118
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   119
            public int readNBytes(byte[] b, int off, int len)
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   120
                throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   121
                Objects.checkFromIndexSize(off, len, b.length);
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   122
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   123
                return 0;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   124
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   125
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   126
            @Override
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   127
            public byte[] readNBytes(int len) throws IOException {
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   128
                if (len < 0) {
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   129
                    throw new IllegalArgumentException("len < 0");
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   130
                }
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   131
                ensureOpen();
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   132
                return new byte[0];
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   133
            }
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   134
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   135
            @Override
48461
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   136
            public long skip(long n) throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   137
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   138
                return 0L;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   139
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   140
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   141
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   142
            public long transferTo(OutputStream out) throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   143
                Objects.requireNonNull(out);
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   144
                ensureOpen();
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   145
                return 0L;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   146
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   147
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   148
            @Override
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   149
            public void close() throws IOException {
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   150
                closed = true;
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   151
            }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   152
        };
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   153
    }
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   154
6a1c3a5e04f3 4358774: Add null InputStream and OutputStream
bpb
parents: 48366
diff changeset
   155
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * Reads the next byte of data from the input stream. The value byte is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * returned as an <code>int</code> in the range <code>0</code> to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * <code>255</code>. If no byte is available because the end of the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * has been reached, the value <code>-1</code> is returned. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * blocks until input data is available, the end of the stream is detected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * <p> A subclass must provide an implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @return     the next byte of data, or <code>-1</code> if the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     *             stream is reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    public abstract int read() throws IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Reads some number of bytes from the input stream and stores them into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * the buffer array <code>b</code>. The number of bytes actually read is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * returned as an integer.  This method blocks until input data is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * available, end of file is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * <p> If the length of <code>b</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * least one byte. If no byte is available because the stream is at the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * end of the file, the value <code>-1</code> is returned; otherwise, at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * least one byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * <p> The first byte read is stored into element <code>b[0]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * next one into <code>b[1]</code>, and so on. The number of bytes read is,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * at most, equal to the length of <code>b</code>. Let <i>k</i> be the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * number of bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * <code>b[0]</code> through <code>b[</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * leaving elements <code>b[</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * <code>b[b.length-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * <p> The <code>read(b)</code> method for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * has the same effect as: <pre><code> read(b, 0, b.length) </code></pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * @param      b   the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @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
   196
     *             <code>-1</code> if there is no more data because the end of
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception  IOException  If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * other than the end of the file, if the input stream has been closed, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * if some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @exception  NullPointerException  if <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @see        java.io.InputStream#read(byte[], int, int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    public int read(byte b[]) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        return read(b, 0, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Reads up to <code>len</code> bytes of data from the input stream into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * an array of bytes.  An attempt is made to read as many as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * <code>len</code> bytes, but a smaller number may be read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * The number of bytes actually read is returned as an integer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p> This method blocks until input data is available, end of file is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * <p> If <code>len</code> is zero, then no bytes are read and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <code>0</code> is returned; otherwise, there is an attempt to read at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * least one byte. If no byte is available because the stream is at end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * file, the value <code>-1</code> is returned; otherwise, at least one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * byte is read and stored into <code>b</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p> The first byte read is stored into element <code>b[off]</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * next one into <code>b[off+1]</code>, and so on. The number of bytes read
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * is, at most, equal to <code>len</code>. Let <i>k</i> be the number of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * bytes actually read; these bytes will be stored in elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <code>b[off]</code> through <code>b[off+</code><i>k</i><code>-1]</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * leaving elements <code>b[off+</code><i>k</i><code>]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * <code>b[off+len-1]</code> unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p> In every case, elements <code>b[0]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * <code>b[off]</code> and elements <code>b[off+len]</code> through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * <code>b[b.length-1]</code> are unaffected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * <p> The <code>read(b,</code> <code>off,</code> <code>len)</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * for class <code>InputStream</code> simply calls the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * <code>read()</code> repeatedly. If the first such call results in an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <code>IOException</code>, that exception is returned from the call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * the <code>read(b,</code> <code>off,</code> <code>len)</code> method.  If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * any subsequent call to <code>read()</code> results in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * <code>IOException</code>, the exception is caught and treated as if it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * were end of file; the bytes read up to that point are stored into
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * <code>b</code> and the number of bytes read before the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * occurred is returned. The default implementation of this method blocks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * until the requested amount of input data <code>len</code> has been read,
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   246
     * end of file is detected, or an exception is thrown. Subclasses are
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   247
     * encouraged to provide a more efficient implementation of this method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @param      b     the buffer into which the data is read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @param      off   the start offset in array <code>b</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * @param      len   the maximum number of bytes to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * @return     the total number of bytes read into the buffer, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *             <code>-1</code> if there is no more data because the end of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *             the stream has been reached.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @exception  IOException If the first byte cannot be read for any reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * other than end of file, or if the input stream has been closed, or if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * some other I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * @exception  NullPointerException If <code>b</code> is <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * @exception  IndexOutOfBoundsException If <code>off</code> is negative,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * <code>len</code> is negative, or <code>len</code> is greater than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <code>b.length - off</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @see        java.io.InputStream#read()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    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
   266
        Objects.checkFromIndexSize(off, len, b.length);
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   267
        if (len == 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        int c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        b[off] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int i = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            for (; i < len ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                c = read();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                if (c == -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                b[off + i] = (byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        } catch (IOException ee) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    /**
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   292
     * The maximum size of array to allocate.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   293
     * Some VMs reserve some header words in an array.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   294
     * Attempts to allocate larger arrays may result in
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   295
     * OutOfMemoryError: Requested array size exceeds VM limit
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   296
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   297
    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
   298
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   299
    /**
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   300
     * 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
   301
     * 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
   302
     * 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
   303
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   304
     * <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
   305
     * method will return an empty byte array.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   306
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   307
     * <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
   308
     * 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
   309
     * reading input streams with large amounts of data.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   310
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   311
     * <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
   312
     * 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
   313
     * stream specific, and therefore not specified.
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
     * <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
   316
     * 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
   317
     * 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
   318
     * 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
   319
     * error occurs.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   320
     *
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   321
     * @implSpec
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   322
     * This method invokes {@link #readNBytes(int)} with a length of
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   323
     * {@link Integer#MAX_VALUE}.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   324
     *
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   325
     * @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
   326
     * @throws IOException if an I/O error occurs
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   327
     * @throws OutOfMemoryError if an array of the required size cannot be
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   328
     *         allocated.
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   329
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   330
     * @since 9
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   331
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   332
    public byte[] readAllBytes() throws IOException {
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   333
        return readNBytes(Integer.MAX_VALUE);
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   334
    }
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   335
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   336
    /**
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   337
     * Reads up to a specified number of bytes from the input stream. This
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   338
     * method blocks until the requested number of bytes have been read, end
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   339
     * of stream is detected, or an exception is thrown. This method does not
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   340
     * close the input stream.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   341
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   342
     * <p> The length of the returned array equals the number of bytes read
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   343
     * from the stream. If {@code len} is zero, then no bytes are read and
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   344
     * an empty byte array is returned. Otherwise, up to {@code len} bytes
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   345
     * are read from the stream. Fewer than {@code len} bytes may be read if
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   346
     * end of stream is encountered.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   347
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   348
     * <p> When this stream reaches end of stream, further invocations of this
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   349
     * method will return an empty byte array.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   350
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   351
     * <p> Note that this method is intended for simple cases where it is
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   352
     * convenient to read the specified number of bytes into a byte array. The
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   353
     * total amount of memory allocated by this method is proportional to the
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   354
     * number of bytes read from the stream which is bounded by {@code len}.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   355
     * Therefore, the method may be safely called with very large values of
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   356
     * {@code len} provided sufficient memory is available.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   357
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   358
     * <p> The behavior for the case where the input stream is <i>asynchronously
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   359
     * closed</i>, or the thread interrupted during the read, is highly input
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   360
     * stream specific, and therefore not specified.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   361
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   362
     * <p> If an I/O error occurs reading from the input stream, then it may do
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   363
     * so after some, but not all, bytes have been read. Consequently the input
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   364
     * stream may not be at end of stream and may be in an inconsistent state.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   365
     * It is strongly recommended that the stream be promptly closed if an I/O
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   366
     * error occurs.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   367
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   368
     * @implNote
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   369
     * The number of bytes allocated to read data from this stream and return
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   370
     * the result is bounded by {@code 2*(long)len}, inclusive.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   371
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   372
     * @param len the maximum number of bytes to read
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   373
     * @return a byte array containing the bytes read from this input stream
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   374
     * @throws IllegalArgumentException if {@code length} is negative
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   375
     * @throws IOException if an I/O error occurs
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   376
     * @throws OutOfMemoryError if an array of the required size cannot be
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   377
     *         allocated.
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   378
     *
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   379
     * @since 11
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   380
     */
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   381
    public byte[] readNBytes(int len) throws IOException {
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   382
        if (len < 0) {
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   383
            throw new IllegalArgumentException("len < 0");
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   384
        }
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   385
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   386
        List<byte[]> bufs = null;
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   387
        byte[] result = null;
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   388
        int total = 0;
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   389
        int remaining = len;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   390
        int n;
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   391
        do {
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   392
            byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   393
            int nread = 0;
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   394
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   395
            // read to EOF which may read more or less than buffer size
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   396
            while ((n = read(buf, nread,
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   397
                    Math.min(buf.length - nread, remaining))) > 0) {
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   398
                nread += n;
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   399
                remaining -= n;
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   400
            }
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   401
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   402
            if (nread > 0) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   403
                if (MAX_BUFFER_SIZE - total < nread) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   404
                    throw new OutOfMemoryError("Required array size too large");
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   405
                }
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   406
                total += nread;
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   407
                if (result == null) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   408
                    result = buf;
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   409
                } else {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   410
                    if (bufs == null) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   411
                        bufs = new ArrayList<>();
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   412
                        bufs.add(result);
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   413
                    }
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   414
                    bufs.add(buf);
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   415
                }
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   416
            }
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   417
            // if the last call to read returned -1 or the number of bytes
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   418
            // requested have been read then break
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   419
        } while (n >= 0 && remaining > 0);
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   420
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   421
        if (bufs == null) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   422
            if (result == null) {
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   423
                return new byte[0];
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   424
            }
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   425
            return result.length == total ?
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   426
                result : Arrays.copyOf(result, total);
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   427
        }
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   428
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   429
        result = new byte[total];
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   430
        int offset = 0;
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   431
        remaining = total;
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   432
        for (byte[] b : bufs) {
48696
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   433
            int count = Math.min(b.length, remaining);
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   434
            System.arraycopy(b, 0, result, offset, count);
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   435
            offset += count;
917868f73209 8139206: Add InputStream readNBytes(int len)
bpb
parents: 48461
diff changeset
   436
            remaining -= count;
48366
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   437
        }
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   438
2c1af559e922 8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents: 47920
diff changeset
   439
        return result;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   440
    }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   441
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   442
    /**
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   443
     * 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
   444
     * 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
   445
     * 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
   446
     * 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
   447
     * does not close the input stream.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   448
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   449
     * <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
   450
     * 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
   451
     * 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
   452
     * method will return zero.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   453
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   454
     * <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
   455
     * 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
   456
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   457
     * <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
   458
     * 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
   459
     * 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
   460
     * 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
   461
     * {@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
   462
     * {@code ]} through {@code b[off+len-1]} unaffected.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   463
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   464
     * <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
   465
     * 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
   466
     * stream specific, and therefore not specified.
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   467
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   468
     * <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
   469
     * 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
   470
     * 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
   471
     * 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
   472
     * 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
   473
     *
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   474
     * @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
   475
     * @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
   476
     * @param  len the maximum number of bytes to read
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   477
     * @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
   478
     * @throws IOException if an I/O error occurs
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   479
     * @throws NullPointerException if {@code b} is {@code null}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   480
     * @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   481
     *         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
   482
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   483
     * @since 9
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   484
     */
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   485
    public int readNBytes(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
   486
        Objects.checkFromIndexSize(off, len, b.length);
52c9e8d2f8d9 8191516: OutputStream.write(byte[],int,int) could have fewer parameter bounds checks
bpb
parents: 47216
diff changeset
   487
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   488
        int n = 0;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   489
        while (n < len) {
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   490
            int count = read(b, off + n, len - n);
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   491
            if (count < 0)
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   492
                break;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   493
            n += count;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   494
        }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   495
        return n;
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   496
    }
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   497
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   498
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * Skips over and discards <code>n</code> bytes of data from this input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * stream. The <code>skip</code> method may, for a variety of reasons, end
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * up skipping over some smaller number of bytes, possibly <code>0</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * This may result from any of a number of conditions; reaching end of file
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * 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
   504
     * The actual number of bytes skipped is returned. If {@code n} is
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   505
     * negative, the {@code skip} method for class {@code InputStream} always
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   506
     * returns 0, and no bytes are skipped. Subclasses may handle the negative
13931cd9405f 8011136: FileInputStream.available and skip inconsistencies
dxu
parents: 14342
diff changeset
   507
     * value differently.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     *
39121
a1a92fce338a 8136738: InputStream documentation for IOException in skip() is unclear or incorrect
bpb
parents: 35302
diff changeset
   509
     * <p> The <code>skip</code> method implementation of this class creates a
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * byte array and then repeatedly reads into it until <code>n</code> bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     * have been read or the end of the stream has been reached. Subclasses are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * encouraged to provide a more efficient implementation of this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * For instance, the implementation may depend on the ability to seek.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
     * @param      n   the number of bytes to be skipped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * @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
   517
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
    public long skip(long n) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
        long remaining = n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
        int nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        if (n <= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   528
        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
   529
        byte[] skipBuffer = new byte[size];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        while (remaining > 0) {
10901
8116ea1694a4 7000600: InputStream.skip() makes sensitive data accessible to malicious code
alanb
parents: 5506
diff changeset
   531
            nr = read(skipBuffer, 0, (int)Math.min(size, remaining));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            if (nr < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            remaining -= nr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
        return n - remaining;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * skipped over) from this input stream without blocking by the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * invocation of a method for this input stream. The next invocation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * might be the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p> Note that while some implementations of {@code InputStream} will return
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * the total number of bytes in the stream, many will not.  It is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * never correct to use the return value of this method to allocate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * a buffer intended to hold all data in this stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     * <p> A subclass' implementation of this method may choose to throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * {@link IOException} if this input stream has been closed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * invoking the {@link #close()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * <p> The {@code available} method for class {@code InputStream} always
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     * returns {@code 0}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * <p> This method should be overridden by subclasses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * @return     an estimate of the number of bytes that can be read (or skipped
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *             over) from this input stream without blocking or {@code 0} when
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *             it reaches the end of the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @exception  IOException if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public int available() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Closes this input stream and releases any system resources associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * with the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p> The <code>close</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @exception  IOException  if an I/O error occurs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
    public void close() throws IOException {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * Marks the current position in this input stream. A subsequent call to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * the <code>reset</code> method repositions this stream at the last marked
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * position so that subsequent reads re-read the same bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * <p> The <code>readlimit</code> arguments tells this input stream to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * allow that many bytes to be read before the mark position gets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * <p> The general contract of <code>mark</code> is that, if the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <code>markSupported</code> returns <code>true</code>, the stream somehow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     * remembers all the bytes read after the call to <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * stands ready to supply those same bytes again if and whenever the method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * <code>reset</code> is called.  However, the stream is not required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * remember any data at all if more than <code>readlimit</code> bytes are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * read from the stream before <code>reset</code> is called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * <p> Marking a closed stream should not have any effect on the stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * <p> The <code>mark</code> method of <code>InputStream</code> does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * @param   readlimit   the maximum limit of bytes that can be read before
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *                      the mark position becomes invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
    public synchronized void mark(int readlimit) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * Repositions this stream to the position at the time the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * <code>mark</code> method was last called on this input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * <p> The general contract of <code>reset</code> is:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 17691
diff changeset
   616
     * <ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * <code>true</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     *     <ul><li> If the method <code>mark</code> has not been called since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     *     the stream was created, or the number of bytes read from the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     *     since <code>mark</code> was last called is larger than the argument
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *     to <code>mark</code> at that last call, then an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     *     <code>IOException</code> might be thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     *     <li> If such an <code>IOException</code> is not thrown, then the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     *     stream is reset to a state such that all the bytes read since the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     *     most recent call to <code>mark</code> (or since the start of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *     file, if <code>mark</code> has not been called) will be resupplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     *     to subsequent callers of the <code>read</code> method, followed by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *     any bytes that otherwise would have been the next input data as of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     *     the time of the call to <code>reset</code>. </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * <li> If the method <code>markSupported</code> returns
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <code>false</code>, then:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *     <ul><li> The call to <code>reset</code> may throw an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *     <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     *     <li> If an <code>IOException</code> is not thrown, then the stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *     is reset to a fixed state that depends on the particular type of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     *     input stream and how it was created. The bytes that will be supplied
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     *     to subsequent callers of the <code>read</code> method depend on the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     *     particular type of the input stream. </ul></ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     * <p>The method <code>reset</code> for class <code>InputStream</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
     * does nothing except throw an <code>IOException</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     * @exception  IOException  if this stream has not been marked or if the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     *               mark has been invalidated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * @see     java.io.IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    public synchronized void reset() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        throw new IOException("mark/reset not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * Tests if this input stream supports the <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * <code>reset</code> methods. Whether or not <code>mark</code> and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <code>reset</code> are supported is an invariant property of a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * particular input stream instance. The <code>markSupported</code> method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * of <code>InputStream</code> returns <code>false</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @return  <code>true</code> if this stream instance supports the mark
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *          and reset methods; <code>false</code> otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @see     java.io.InputStream#mark(int)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @see     java.io.InputStream#reset()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public boolean markSupported() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   674
    /**
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   675
     * 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
   676
     * 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
   677
     * 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
   678
     * stream.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   679
     * <p>
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   680
     * 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
   681
     * 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
   682
     * 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
   683
     * 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
   684
     * specific, and therefore not specified.
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   685
     * <p>
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   686
     * 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
   687
     * 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
   688
     * 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
   689
     * 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
   690
     * 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
   691
     *
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   692
     * @param  out the output stream, non-null
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   693
     * @return the number of bytes transferred
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   694
     * @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
   695
     * @throws NullPointerException if {@code out} is {@code null}
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   696
     *
35302
e4d2275861c3 8136494: Update "@since 1.9" to "@since 9" to match java.version.specification
iris
parents: 30962
diff changeset
   697
     * @since 9
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   698
     */
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   699
    public long transferTo(OutputStream out) throws IOException {
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   700
        Objects.requireNonNull(out, "out");
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   701
        long transferred = 0;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   702
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
28065
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   703
        int read;
30962
3d58028e8e7f 8080835: Add blocking bulk read to java.io.InputStream
chegar
parents: 28065
diff changeset
   704
        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
   705
            out.write(buffer, 0, read);
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   706
            transferred += read;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   707
        }
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   708
        return transferred;
b35a5a7f6dcc 8066867: Add InputStream transferTo to transfer content to an OutputStream
prappo
parents: 25859
diff changeset
   709
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
}