author | chegar |
Thu, 17 Oct 2019 20:54:25 +0100 | |
branch | datagramsocketimpl-branch |
changeset 58679 | 9c3209ff7550 |
parent 58678 | 9cf78a70fa4f |
parent 58288 | 48e480e56aad |
permissions | -rw-r--r-- |
2 | 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 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.io; |
|
27 |
||
28 |
import java.io.InputStream; |
|
29 |
import java.util.Enumeration; |
|
30 |
import java.util.Vector; |
|
31 |
||
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 | 34 |
* the logical concatenation of other input |
35 |
* streams. It starts out with an ordered |
|
36 |
* collection of input streams and reads from |
|
37 |
* the first one until end of file is reached, |
|
38 |
* whereupon it reads from the second one, |
|
39 |
* and so on, until end of file is reached |
|
40 |
* on the last of the contained input streams. |
|
41 |
* |
|
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 | 44 |
*/ |
45 |
public |
|
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 | 48 |
InputStream in; |
49 |
||
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 | 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 | 55 |
* The input streams that are produced by |
56 |
* the enumeration will be read, in order, |
|
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 | 59 |
* each input stream from the enumeration |
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 | 62 |
* |
63 |
* @param e an enumeration of input streams. |
|
64 |
* @see java.util.Enumeration |
|
65 |
*/ |
|
66 |
public SequenceInputStream(Enumeration<? extends InputStream> e) { |
|
67 |
this.e = e; |
|
40410 | 68 |
peekNextStream(); |
2 | 69 |
} |
70 |
||
71 |
/** |
|
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 | 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 | 78 |
* |
79 |
* @param s1 the first input stream to read. |
|
80 |
* @param s2 the second input stream to read. |
|
81 |
*/ |
|
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 | 84 |
v.addElement(s1); |
85 |
v.addElement(s2); |
|
86 |
e = v.elements(); |
|
40410 | 87 |
peekNextStream(); |
2 | 88 |
} |
89 |
||
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 | 92 |
*/ |
93 |
final void nextStream() throws IOException { |
|
94 |
if (in != null) { |
|
95 |
in.close(); |
|
96 |
} |
|
40410 | 97 |
peekNextStream(); |
98 |
} |
|
2 | 99 |
|
40410 | 100 |
private void peekNextStream() { |
2 | 101 |
if (e.hasMoreElements()) { |
102 |
in = (InputStream) e.nextElement(); |
|
103 |
if (in == null) |
|
104 |
throw new NullPointerException(); |
|
40410 | 105 |
} else { |
106 |
in = null; |
|
2 | 107 |
} |
108 |
} |
|
109 |
||
110 |
/** |
|
111 |
* Returns an estimate of the number of bytes that can be read (or |
|
112 |
* skipped over) from the current underlying input stream without |
|
113 |
* blocking by the next invocation of a method for the current |
|
114 |
* underlying input stream. The next invocation might be |
|
115 |
* the same thread or another thread. A single read or skip of this |
|
116 |
* many bytes will not block, but may read or skip fewer bytes. |
|
117 |
* <p> |
|
118 |
* This method simply calls {@code available} of the current underlying |
|
119 |
* input stream and returns the result. |
|
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 | 126 |
* |
58242
94bb65cb37d3
8230648: Replace @exception tag with @throws in java.base
jboes
parents:
57569
diff
changeset
|
127 |
* @since 1.1 |
2 | 128 |
*/ |
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 | 131 |
return 0; // no way to signal EOF from available() |
132 |
} |
|
133 |
return in.available(); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
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 | 141 |
* This method blocks until input data is available, the end of the |
142 |
* stream is detected, or an exception is thrown. |
|
143 |
* <p> |
|
144 |
* This method |
|
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 | 147 |
* method of the current substream and begins reading from the next |
148 |
* substream. |
|
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 | 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 | 153 |
*/ |
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 | 161 |
} |
23344
70f819173479
7011804: SequenceInputStream with lots of empty substreams can cause StackOverflowError
igerasim
parents:
14342
diff
changeset
|
162 |
return -1; |
2 | 163 |
} |
164 |
||
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 | 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 | 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 | 172 |
* tries to read the data from the current substream. If it fails to |
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 | 175 |
* substream and begins reading from the next substream. |
176 |
* |
|
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 | 179 |
* at which the data is written. |
180 |
* @param len the maximum number of bytes read. |
|
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 | 187 |
*/ |
188 |
public int read(byte b[], int off, int len) throws IOException { |
|
189 |
if (in == null) { |
|
190 |
return -1; |
|
191 |
} else if (b == null) { |
|
192 |
throw new NullPointerException(); |
|
193 |
} else if (off < 0 || len < 0 || len > b.length - off) { |
|
194 |
throw new IndexOutOfBoundsException(); |
|
195 |
} else if (len == 0) { |
|
196 |
return 0; |
|
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 | 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 | 206 |
} |
207 |
||
208 |
/** |
|
209 |
* Closes this input stream and releases any system resources |
|
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 | 212 |
* cannot perform input operations and cannot |
213 |
* be reopened. |
|
214 |
* <p> |
|
215 |
* If this stream was created |
|
216 |
* from an enumeration, all remaining elements |
|
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 | 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 | 221 |
*/ |
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 | 239 |
} |
240 |
} |