src/java.base/share/classes/java/io/SequenceInputStream.java
author jboes
Thu, 21 Nov 2019 09:10:21 +0000
changeset 59201 b24f4caa1411
parent 58288 48e480e56aad
permissions -rw-r--r--
8234335: Remove line break in class declaration in java.base Summary: Remove line break in class declarations where applicable Reviewed-by: rriggs, lancea
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
 */
59201
b24f4caa1411 8234335: Remove line break in class declaration in java.base
jboes
parents: 58288
diff changeset
    45
public class SequenceInputStream extends InputStream {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
    46
    Enumeration<? extends InputStream> e;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    InputStream in;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
    50
     * Initializes a newly created {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
     * 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
    52
     * 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
    53
     * objects whose run-time type is {@code InputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The input streams that are  produced by
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * the enumeration will be read, in order,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * 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
    57
     * {@code SequenceInputStream}. After
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * each input stream from the enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * 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
    60
     * {@code close} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * @param   e   an enumeration of input streams.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * @see     java.util.Enumeration
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    public SequenceInputStream(Enumeration<? extends InputStream> e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        this.e = e;
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    67
        peekNextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * 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
    72
     * created {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * 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
    74
     * 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
    75
     * 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
    76
     * bytes to be read from this {@code SequenceInputStream}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param   s1   the first input stream to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * @param   s2   the second input stream to read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public SequenceInputStream(InputStream s1, InputStream s2) {
11117
b6e68b1344d4 7116404: Miscellaneous warnings (java.rmi.**, serialization, some core classes)
alanb
parents: 5506
diff changeset
    82
        Vector<InputStream> v = new Vector<>(2);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        v.addElement(s1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        v.addElement(s2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        e = v.elements();
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    86
        peekNextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    /**
57569
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
    90
     * Continues reading in the next stream if an EOF is reached.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    final void nextStream() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    96
        peekNextStream();
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    97
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
    99
    private void peekNextStream() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            in = (InputStream) e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            if (in == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                throw new NullPointerException();
40410
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
   104
        } else {
5fd4a1f809f8 8163517: Various cleanup in java.io code
igerasim
parents: 25859
diff changeset
   105
            in = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
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
     * Returns an estimate of the number of bytes that can be read (or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * skipped over) from the current underlying input stream without
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * blocking by the next invocation of a method for the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * underlying input stream. The next invocation might be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * the same thread or another thread.  A single read or skip of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * many bytes will not block, but may read or skip fewer bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * This method simply calls {@code available} of the current underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * input stream and returns the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   120
     * @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
   121
     *           skipped over) from the current underlying input stream
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   122
     *           without blocking or {@code 0} if this input stream
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   123
     *           has been closed by invoking its {@link #close()} method
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   124
     * @throw    IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   126
     * @since    1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    public int available() throws IOException {
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   129
        if (in == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return 0; // no way to signal EOF from available()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return in.available();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * 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
   137
     * 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
   138
     * {@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
   139
     * stream has been reached, the value {@code -1} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * This method blocks until input data is available, the end of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * stream is detected, or an exception is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * 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
   145
     * reaches the end of the stream, it calls the {@code close}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * method of the current substream and begins reading from the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * substream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   149
     * @return     the next byte of data, or {@code -1} if the end of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *             stream is reached.
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   151
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public int read() throws IOException {
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   154
        while (in != null) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   155
            int c = in.read();
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   156
            if (c != -1) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   157
                return c;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   158
            }
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   159
            nextStream();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   161
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    /**
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   165
     * 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
   166
     * into an array of bytes.  If {@code len} is not zero, the method
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * 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
   168
     * bytes are read and {@code 0} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <p>
58288
48e480e56aad 8231186: Replace html tag <code>foo</code> with javadoc tag {@code foo} in java.base
jboes
parents: 58242
diff changeset
   170
     * The {@code read} method of {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * tries to read the data from the current substream. If it fails to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * 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
   173
     * the stream, it calls the {@code close} method of the current
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * substream and begins reading from the next substream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * @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
   177
     * @param      off   the start offset in array {@code b}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *                   at which the data is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @param      len   the maximum number of bytes read.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * @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
   181
     * @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
   182
     * @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
   183
     *             {@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
   184
     *             greater than {@code b.length - off}
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   185
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    public int read(byte b[], int off, int len) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (in == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        } else if (b == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        } else if (off < 0 || len < 0 || len > b.length - off) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            throw new IndexOutOfBoundsException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        } else if (len == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   197
        do {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   198
            int n = in.read(b, off, len);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   199
            if (n > 0) {
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   200
                return n;
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   201
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            nextStream();
23344
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   203
        } while (in != null);
70f819173479 7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents: 14342
diff changeset
   204
        return -1;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * Closes this input stream and releases any system resources
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * 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
   210
     * A closed {@code SequenceInputStream}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * cannot  perform input operations and cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * be reopened.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * If this stream was created
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * from an enumeration, all remaining elements
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * 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
   217
     * before the {@code close} method returns.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
58242
94bb65cb37d3 8230648: Replace @exception tag with @throws in java.base
jboes
parents: 57569
diff changeset
   219
     * @throws     IOException  if an I/O error occurs.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    public void close() throws IOException {
57569
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   222
        IOException ioe = null;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   223
        while (in != null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   224
            try {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   225
                in.close();
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   226
            } catch (IOException e) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   227
                if (ioe == null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   228
                    ioe = e;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   229
                } else {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   230
                    ioe.addSuppressed(e);
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   231
                }
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
            peekNextStream();
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   234
        }
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   235
        if (ioe != null) {
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   236
            throw ioe;
be47f3ccdf12 8078891: java.io.SequenceInputStream.close is not atomic and not idempotent
bpb
parents: 47216
diff changeset
   237
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
}