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