src/java.base/share/classes/java/io/SequenceInputStream.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
/*
57569
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1994, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.Vector;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    33
 * A {@code SequenceInputStream} represents
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * the logical concatenation of other input
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * streams. It starts out with an ordered
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * collection of input streams and reads from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * the first one until end of file is reached,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * whereupon it reads from the second one,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * and so on, until end of file is reached
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * on the last of the contained input streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @author  Author van Hoff
24865
09b1d992ca72 8044740: Convert all JDK versions used in @since tag to 1.n[.n] in jdk repo
henryjen
parents: 23344
diff changeset
    43
 * @since   1.0
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
public
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class SequenceInputStream extends InputStream {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
    47
    Enumeration<? extends InputStream> e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    51
     * Initializes a newly created {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * by remembering the argument, which must
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    53
     * be an {@code Enumeration}  that produces
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    54
     * objects whose run-time type is {@code InputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * The input streams that are  produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * the enumeration will be read, in order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * to provide the bytes to be read  from this
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    58
     * {@code SequenceInputStream}. After
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * each input stream from the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * is exhausted, it is closed by calling its
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    61
     * {@code close} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @param   e   an enumeration of input streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public SequenceInputStream(Enumeration<? extends InputStream> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        this.e = e;
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    68
        peekNextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * Initializes a newly
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    73
     * created {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * by remembering the two arguments, which
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    75
     * will be read in order, first {@code s1}
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    76
     * and then {@code s2}, to provide the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    77
     * bytes to be read from this {@code SequenceInputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param   s1   the first input stream to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param   s2   the second input stream to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    public SequenceInputStream(InputStream s1, InputStream s2) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
    83
        Vector<InputStream> v = new Vector<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        v.addElement(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        v.addElement(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        e = v.elements();
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    87
        peekNextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
57569
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
    91
     * Continues reading in the next stream if an EOF is reached.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    final void nextStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    97
        peekNextStream();
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    98
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
   100
    private void peekNextStream() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            in = (InputStream) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                throw new NullPointerException();
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
   105
        } else {
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
   106
            in = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * skipped over) from the current underlying input stream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * blocking by the next invocation of a method for the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * underlying input stream. The next invocation might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * This method simply calls {@code available} of the current underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * input stream and returns the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   121
     * @return   an estimate of the number of bytes that can be read (or
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   122
     *           skipped over) from the current underlying input stream
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   123
     *           without blocking or {@code 0} if this input stream
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   124
     *           has been closed by invoking its {@link #close()} method
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   125
     * @throw    IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   127
     * @since    1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    public int available() throws IOException {
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   130
        if (in == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            return 0; // no way to signal EOF from available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * Reads the next byte of data from this input stream. The byte is
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   138
     * 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
   139
     * {@code 255}. If no byte is available because the end of the
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   140
     * stream has been reached, the value {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * This method blocks until input data is available, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * tries to read one character from the current substream. If it
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   146
     * reaches the end of the stream, it calls the {@code close}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * method of the current substream and begins reading from the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * substream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   150
     * @return     the next byte of data, or {@code -1} if the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *             stream is reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   152
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    public int read() throws IOException {
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   155
        while (in != null) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   156
            int c = in.read();
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   157
            if (c != -1) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   158
                return c;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   159
            }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   160
            nextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   162
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   166
     * Reads up to {@code len} bytes of data from this input stream
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   167
     * into an array of bytes.  If {@code len} is not zero, the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * blocks until at least 1 byte of input is available; otherwise, no
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   169
     * bytes are read and {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   171
     * The {@code read} method of {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * tries to read the data from the current substream. If it fails to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * read any characters because the substream has reached the end of
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   174
     * the stream, it calls the {@code close} method of the current
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * substream and begins reading from the next substream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @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
   178
     * @param      off   the start offset in array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @return     int   the number of bytes read.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   182
     * @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
   183
     * @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
   184
     *             {@code len} is negative, or {@code len} is
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   185
     *             greater than {@code b.length - off}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   186
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        } else if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        }
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   198
        do {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   199
            int n = in.read(b, off, len);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   200
            if (n > 0) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   201
                return n;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   202
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            nextStream();
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   204
        } while (in != null);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   205
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * associated with the stream.
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   211
     * A closed {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * cannot  perform input operations and cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * be reopened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * If this stream was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * from an enumeration, all remaining elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * are requested from the enumeration and closed
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   218
     * before the {@code close} method returns.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   220
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
    public void close() throws IOException {
57569
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   223
        IOException ioe = null;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   224
        while (in != null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   225
            try {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   226
                in.close();
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   227
            } catch (IOException e) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   228
                if (ioe == null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   229
                    ioe = e;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   230
                } else {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   231
                    ioe.addSuppressed(e);
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   232
                }
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   233
            }
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   234
            peekNextStream();
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   235
        }
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   236
        if (ioe != null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   237
            throw ioe;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   238
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
}