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 |
/* |
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 | 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 |
||
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 | 33 |
/** |
34 |
* This abstract class is the superclass of all classes representing |
|
35 |
* an input stream of bytes. |
|
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 | 38 |
* must always provide a method that returns the next byte of input. |
39 |
* |
|
40 |
* @author Arthur van Hoff |
|
41 |
* @see java.io.BufferedInputStream |
|
42 |
* @see java.io.ByteArrayInputStream |
|
43 |
* @see java.io.DataInputStream |
|
44 |
* @see java.io.FilterInputStream |
|
45 |
* @see java.io.InputStream#read() |
|
46 |
* @see java.io.OutputStream |
|
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 | 49 |
*/ |
50 |
public abstract class InputStream implements Closeable { |
|
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 | 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 | 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 | 64 |
* Returns a new {@code InputStream} that reads no bytes. The returned |
65 |
* stream is initially open. The stream is closed by calling the |
|
66 |
* {@code close()} method. Subsequent calls to {@code close()} have no |
|
67 |
* effect. |
|
68 |
* |
|
69 |
* <p> While the stream is open, the {@code available()}, {@code read()}, |
|
70 |
* {@code read(byte[])}, {@code read(byte[], int, int)}, |
|
48696 | 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 | 74 |
* reached. After the stream has been closed, these methods all throw |
75 |
* {@code IOException}. |
|
76 |
* |
|
77 |
* <p> The {@code markSupported()} method returns {@code false}. The |
|
78 |
* {@code mark()} method does nothing, and the {@code reset()} method |
|
79 |
* throws {@code IOException}. |
|
80 |
* |
|
81 |
* @return an {@code InputStream} which contains no bytes |
|
82 |
* |
|
83 |
* @since 11 |
|
84 |
*/ |
|
85 |
public static InputStream nullInputStream() { |
|
86 |
return new InputStream() { |
|
87 |
private volatile boolean closed; |
|
88 |
||
89 |
private void ensureOpen() throws IOException { |
|
90 |
if (closed) { |
|
91 |
throw new IOException("Stream closed"); |
|
92 |
} |
|
93 |
} |
|
94 |
||
95 |
@Override |
|
96 |
public int available () throws IOException { |
|
97 |
ensureOpen(); |
|
98 |
return 0; |
|
99 |
} |
|
100 |
||
101 |
@Override |
|
102 |
public int read() throws IOException { |
|
103 |
ensureOpen(); |
|
104 |
return -1; |
|
105 |
} |
|
106 |
||
107 |
@Override |
|
108 |
public int read(byte[] b, int off, int len) throws IOException { |
|
109 |
Objects.checkFromIndexSize(off, len, b.length); |
|
110 |
if (len == 0) { |
|
111 |
return 0; |
|
112 |
} |
|
113 |
ensureOpen(); |
|
114 |
return -1; |
|
115 |
} |
|
116 |
||
117 |
@Override |
|
118 |
public byte[] readAllBytes() throws IOException { |
|
119 |
ensureOpen(); |
|
120 |
return new byte[0]; |
|
121 |
} |
|
122 |
||
123 |
@Override |
|
124 |
public int readNBytes(byte[] b, int off, int len) |
|
125 |
throws IOException { |
|
126 |
Objects.checkFromIndexSize(off, len, b.length); |
|
127 |
ensureOpen(); |
|
128 |
return 0; |
|
129 |
} |
|
130 |
||
131 |
@Override |
|
48696 | 132 |
public byte[] readNBytes(int len) throws IOException { |
133 |
if (len < 0) { |
|
134 |
throw new IllegalArgumentException("len < 0"); |
|
135 |
} |
|
136 |
ensureOpen(); |
|
137 |
return new byte[0]; |
|
138 |
} |
|
139 |
||
140 |
@Override |
|
48461 | 141 |
public long skip(long n) throws IOException { |
142 |
ensureOpen(); |
|
143 |
return 0L; |
|
144 |
} |
|
145 |
||
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 | 155 |
public long transferTo(OutputStream out) throws IOException { |
156 |
Objects.requireNonNull(out); |
|
157 |
ensureOpen(); |
|
158 |
return 0L; |
|
159 |
} |
|
160 |
||
161 |
@Override |
|
162 |
public void close() throws IOException { |
|
163 |
closed = true; |
|
164 |
} |
|
165 |
}; |
|
166 |
} |
|
167 |
||
168 |
/** |
|
2 | 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 | 173 |
* blocks until input data is available, the end of the stream is detected, |
174 |
* or an exception is thrown. |
|
175 |
* |
|
176 |
* <p> A subclass must provide an implementation of this method. |
|
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 | 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 | 181 |
*/ |
182 |
public abstract int read() throws IOException; |
|
183 |
||
184 |
/** |
|
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 | 187 |
* returned as an integer. This method blocks until input data is |
188 |
* available, end of file is detected, or an exception is thrown. |
|
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 | 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 | 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 | 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 | 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 | 206 |
* |
207 |
* @param b the buffer into which the data is read. |
|
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 | 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 | 215 |
* @see java.io.InputStream#read(byte[], int, int) |
216 |
*/ |
|
217 |
public int read(byte b[]) throws IOException { |
|
218 |
return read(b, 0, b.length); |
|
219 |
} |
|
220 |
||
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 | 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 | 225 |
* The number of bytes actually read is returned as an integer. |
226 |
* |
|
227 |
* <p> This method blocks until input data is available, end of file is |
|
228 |
* detected, or an exception is thrown. |
|
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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 259 |
* end of file is detected, or an exception is thrown. Subclasses are |
260 |
* encouraged to provide a more efficient implementation of this method. |
|
2 | 261 |
* |
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 | 264 |
* at which the data is written. |
265 |
* @param len the maximum number of bytes to read. |
|
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 | 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 | 276 |
* @see java.io.InputStream#read() |
277 |
*/ |
|
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 | 281 |
return 0; |
282 |
} |
|
283 |
||
284 |
int c = read(); |
|
285 |
if (c == -1) { |
|
286 |
return -1; |
|
287 |
} |
|
288 |
b[off] = (byte)c; |
|
289 |
||
290 |
int i = 1; |
|
291 |
try { |
|
292 |
for (; i < len ; i++) { |
|
293 |
c = read(); |
|
294 |
if (c == -1) { |
|
295 |
break; |
|
296 |
} |
|
297 |
b[off + i] = (byte)c; |
|
298 |
} |
|
299 |
} catch (IOException ee) { |
|
300 |
} |
|
301 |
return i; |
|
302 |
} |
|
303 |
||
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 | 334 |
* @implSpec |
335 |
* This method invokes {@link #readNBytes(int)} with a length of |
|
336 |
* {@link Integer#MAX_VALUE}. |
|
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 | 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 | 346 |
return readNBytes(Integer.MAX_VALUE); |
347 |
} |
|
348 |
||
349 |
/** |
|
350 |
* Reads up to a specified number of bytes from the input stream. This |
|
351 |
* method blocks until the requested number of bytes have been read, end |
|
352 |
* of stream is detected, or an exception is thrown. This method does not |
|
353 |
* close the input stream. |
|
354 |
* |
|
355 |
* <p> The length of the returned array equals the number of bytes read |
|
356 |
* from the stream. If {@code len} is zero, then no bytes are read and |
|
357 |
* an empty byte array is returned. Otherwise, up to {@code len} bytes |
|
358 |
* are read from the stream. Fewer than {@code len} bytes may be read if |
|
359 |
* end of stream is encountered. |
|
360 |
* |
|
361 |
* <p> When this stream reaches end of stream, further invocations of this |
|
362 |
* method will return an empty byte array. |
|
363 |
* |
|
364 |
* <p> Note that this method is intended for simple cases where it is |
|
365 |
* convenient to read the specified number of bytes into a byte array. The |
|
366 |
* total amount of memory allocated by this method is proportional to the |
|
367 |
* number of bytes read from the stream which is bounded by {@code len}. |
|
368 |
* Therefore, the method may be safely called with very large values of |
|
369 |
* {@code len} provided sufficient memory is available. |
|
370 |
* |
|
371 |
* <p> The behavior for the case where the input stream is <i>asynchronously |
|
372 |
* closed</i>, or the thread interrupted during the read, is highly input |
|
373 |
* stream specific, and therefore not specified. |
|
374 |
* |
|
375 |
* <p> If an I/O error occurs reading from the input stream, then it may do |
|
376 |
* so after some, but not all, bytes have been read. Consequently the input |
|
377 |
* stream may not be at end of stream and may be in an inconsistent state. |
|
378 |
* It is strongly recommended that the stream be promptly closed if an I/O |
|
379 |
* error occurs. |
|
380 |
* |
|
381 |
* @implNote |
|
382 |
* The number of bytes allocated to read data from this stream and return |
|
383 |
* the result is bounded by {@code 2*(long)len}, inclusive. |
|
384 |
* |
|
385 |
* @param len the maximum number of bytes to read |
|
386 |
* @return a byte array containing the bytes read from this input stream |
|
387 |
* @throws IllegalArgumentException if {@code length} is negative |
|
388 |
* @throws IOException if an I/O error occurs |
|
389 |
* @throws OutOfMemoryError if an array of the required size cannot be |
|
390 |
* allocated. |
|
391 |
* |
|
392 |
* @since 11 |
|
393 |
*/ |
|
394 |
public byte[] readNBytes(int len) throws IOException { |
|
395 |
if (len < 0) { |
|
396 |
throw new IllegalArgumentException("len < 0"); |
|
397 |
} |
|
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 | 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 | 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 | 409 |
while ((n = read(buf, nread, |
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 | 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 | 430 |
// if the last call to read returned -1 or the number of bytes |
431 |
// requested have been read then break |
|
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 | 444 |
remaining = total; |
48366
2c1af559e922
8193832: Performance of InputStream.readAllBytes() could be improved
bpb
parents:
47920
diff
changeset
|
445 |
for (byte[] b : bufs) { |
48696 | 446 |
int count = Math.min(b.length, remaining); |
447 |
System.arraycopy(b, 0, result, offset, count); |
|
448 |
offset += count; |
|
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 | 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 | 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 | 524 |
* have been read or the end of the stream has been reached. Subclasses are |
525 |
* encouraged to provide a more efficient implementation of this method. |
|
526 |
* For instance, the implementation may depend on the ability to seek. |
|
527 |
* |
|
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 | 532 |
*/ |
533 |
public long skip(long n) throws IOException { |
|
534 |
long remaining = n; |
|
535 |
int nr; |
|
536 |
||
537 |
if (n <= 0) { |
|
538 |
return 0; |
|
539 |
} |
|
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 | 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 | 545 |
if (nr < 0) { |
546 |
break; |
|
547 |
} |
|
548 |
remaining -= nr; |
|
549 |
} |
|
550 |
||
551 |
return n - remaining; |
|
552 |
} |
|
553 |
||
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 | 614 |
* Returns an estimate of the number of bytes that can be read (or skipped |
615 |
* over) from this input stream without blocking, which may be 0, or 0 when |
|
616 |
* end of stream is detected. The read might be on the same thread or |
|
617 |
* another thread. A single read or skip of this many bytes will not block, |
|
618 |
* but may read or skip fewer bytes. |
|
2 | 619 |
* |
49238 | 620 |
* <p> Note that while some implementations of {@code InputStream} will |
621 |
* return the total number of bytes in the stream, many will not. It is |
|
2 | 622 |
* never correct to use the return value of this method to allocate |
623 |
* a buffer intended to hold all data in this stream. |
|
624 |
* |
|
49238 | 625 |
* <p> A subclass's implementation of this method may choose to throw an |
626 |
* {@link IOException} if this input stream has been closed by invoking the |
|
627 |
* {@link #close()} method. |
|
2 | 628 |
* |
49238 | 629 |
* <p> The {@code available} method of {@code InputStream} always returns |
630 |
* {@code 0}. |
|
2 | 631 |
* |
632 |
* <p> This method should be overridden by subclasses. |
|
633 |
* |
|
49238 | 634 |
* @return an estimate of the number of bytes that can be read (or |
635 |
* skipped over) from this input stream without blocking or |
|
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 | 638 |
*/ |
639 |
public int available() throws IOException { |
|
640 |
return 0; |
|
641 |
} |
|
642 |
||
643 |
/** |
|
644 |
* Closes this input stream and releases any system resources associated |
|
645 |
* with the stream. |
|
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 | 648 |
* nothing. |
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 | 651 |
*/ |
652 |
public void close() throws IOException {} |
|
653 |
||
654 |
/** |
|
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 | 657 |
* position so that subsequent reads re-read the same bytes. |
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 | 660 |
* allow that many bytes to be read before the mark position gets |
661 |
* invalidated. |
|
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 | 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 | 670 |
* |
671 |
* <p> Marking a closed stream should not have any effect on the stream. |
|
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 | 674 |
* nothing. |
675 |
* |
|
676 |
* @param readlimit the maximum limit of bytes that can be read before |
|
677 |
* the mark position becomes invalid. |
|
678 |
* @see java.io.InputStream#reset() |
|
679 |
*/ |
|
680 |
public synchronized void mark(int readlimit) {} |
|
681 |
||
682 |
/** |
|
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 | 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 | 687 |
* |
21334 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 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 | 713 |
* is reset to a fixed state that depends on the particular type of the |
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 | 716 |
* particular type of the input stream. </ul></ul> |
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 | 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 | 723 |
* @see java.io.InputStream#mark(int) |
724 |
* @see java.io.IOException |
|
725 |
*/ |
|
726 |
public synchronized void reset() throws IOException { |
|
727 |
throw new IOException("mark/reset not supported"); |
|
728 |
} |
|
729 |
||
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 | 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 | 739 |
* @see java.io.InputStream#mark(int) |
740 |
* @see java.io.InputStream#reset() |
|
741 |
*/ |
|
742 |
public boolean markSupported() { |
|
743 |
return false; |
|
744 |
} |
|
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 | 782 |
} |