Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/IOUtils.java
38829 views
1
/*
2
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
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.
24
*/
25
26
/**
27
* IOUtils: A collection of IO-related public static methods.
28
*/
29
30
package sun.misc;
31
32
import java.io.EOFException;
33
import java.io.IOException;
34
import java.io.InputStream;
35
36
import java.util.ArrayList;
37
import java.util.Arrays;
38
import java.util.List;
39
import java.util.Objects;
40
41
public class IOUtils {
42
43
private static final int DEFAULT_BUFFER_SIZE = 8192;
44
45
/**
46
* The maximum size of array to allocate.
47
* Some VMs reserve some header words in an array.
48
* Attempts to allocate larger arrays may result in
49
* OutOfMemoryError: Requested array size exceeds VM limit
50
*/
51
private static final int MAX_BUFFER_SIZE = Integer.MAX_VALUE - 8;
52
53
/**
54
* Read exactly {@code length} of bytes from {@code in}.
55
*
56
* <p> Note that this method is safe to be called with unknown large
57
* {@code length} argument. The memory used is proportional to the
58
* actual bytes available. An exception is thrown if there are not
59
* enough bytes in the stream.
60
*
61
* @param is input stream, must not be null
62
* @param length number of bytes to read
63
* @return bytes read
64
* @throws EOFException if there are not enough bytes in the stream
65
* @throws IOException if an I/O error occurs or {@code length} is negative
66
* @throws OutOfMemoryError if an array of the required size cannot be
67
* allocated.
68
*/
69
public static byte[] readExactlyNBytes(InputStream is, int length)
70
throws IOException {
71
if (length < 0) {
72
throw new IOException("length cannot be negative: " + length);
73
}
74
byte[] data = readNBytes(is, length);
75
if (data.length < length) {
76
throw new EOFException();
77
}
78
return data;
79
}
80
81
/**
82
* Reads all remaining bytes from the input stream. This method blocks until
83
* all remaining bytes have been read and end of stream is detected, or an
84
* exception is thrown. This method does not close the input stream.
85
*
86
* <p> When this stream reaches end of stream, further invocations of this
87
* method will return an empty byte array.
88
*
89
* <p> Note that this method is intended for simple cases where it is
90
* convenient to read all bytes into a byte array. It is not intended for
91
* reading input streams with large amounts of data.
92
*
93
* <p> The behavior for the case where the input stream is <i>asynchronously
94
* closed</i>, or the thread interrupted during the read, is highly input
95
* stream specific, and therefore not specified.
96
*
97
* <p> If an I/O error occurs reading from the input stream, then it may do
98
* so after some, but not all, bytes have been read. Consequently the input
99
* stream may not be at end of stream and may be in an inconsistent state.
100
* It is strongly recommended that the stream be promptly closed if an I/O
101
* error occurs.
102
*
103
* @implSpec
104
* This method invokes {@link #readNBytes(int)} with a length of
105
* {@link Integer#MAX_VALUE}.
106
*
107
* @param is input stream, must not be null
108
* @return a byte array containing the bytes read from this input stream
109
* @throws IOException if an I/O error occurs
110
* @throws OutOfMemoryError if an array of the required size cannot be
111
* allocated.
112
*
113
* @since 1.9
114
*/
115
public static byte[] readAllBytes(InputStream is) throws IOException {
116
return readNBytes(is, Integer.MAX_VALUE);
117
}
118
119
/**
120
* Reads up to a specified number of bytes from the input stream. This
121
* method blocks until the requested number of bytes have been read, end
122
* of stream is detected, or an exception is thrown. This method does not
123
* close the input stream.
124
*
125
* <p> The length of the returned array equals the number of bytes read
126
* from the stream. If {@code len} is zero, then no bytes are read and
127
* an empty byte array is returned. Otherwise, up to {@code len} bytes
128
* are read from the stream. Fewer than {@code len} bytes may be read if
129
* end of stream is encountered.
130
*
131
* <p> When this stream reaches end of stream, further invocations of this
132
* method will return an empty byte array.
133
*
134
* <p> Note that this method is intended for simple cases where it is
135
* convenient to read the specified number of bytes into a byte array. The
136
* total amount of memory allocated by this method is proportional to the
137
* number of bytes read from the stream which is bounded by {@code len}.
138
* Therefore, the method may be safely called with very large values of
139
* {@code len} provided sufficient memory is available.
140
*
141
* <p> The behavior for the case where the input stream is <i>asynchronously
142
* closed</i>, or the thread interrupted during the read, is highly input
143
* stream specific, and therefore not specified.
144
*
145
* <p> If an I/O error occurs reading from the input stream, then it may do
146
* so after some, but not all, bytes have been read. Consequently the input
147
* stream may not be at end of stream and may be in an inconsistent state.
148
* It is strongly recommended that the stream be promptly closed if an I/O
149
* error occurs.
150
*
151
* @implNote
152
* The number of bytes allocated to read data from this stream and return
153
* the result is bounded by {@code 2*(long)len}, inclusive.
154
*
155
* @param is input stream, must not be null
156
* @param len the maximum number of bytes to read
157
* @return a byte array containing the bytes read from this input stream
158
* @throws IllegalArgumentException if {@code length} is negative
159
* @throws IOException if an I/O error occurs
160
* @throws OutOfMemoryError if an array of the required size cannot be
161
* allocated.
162
*
163
* @since 11
164
*/
165
public static byte[] readNBytes(InputStream is, int len) throws IOException {
166
if (len < 0) {
167
throw new IllegalArgumentException("len < 0");
168
}
169
170
List<byte[]> bufs = null;
171
byte[] result = null;
172
int total = 0;
173
int remaining = len;
174
int n;
175
do {
176
byte[] buf = new byte[Math.min(remaining, DEFAULT_BUFFER_SIZE)];
177
int nread = 0;
178
179
// read to EOF which may read more or less than buffer size
180
while ((n = is.read(buf, nread,
181
Math.min(buf.length - nread, remaining))) > 0) {
182
nread += n;
183
remaining -= n;
184
}
185
186
if (nread > 0) {
187
if (MAX_BUFFER_SIZE - total < nread) {
188
throw new OutOfMemoryError("Required array size too large");
189
}
190
total += nread;
191
if (result == null) {
192
result = buf;
193
} else {
194
if (bufs == null) {
195
bufs = new ArrayList<>();
196
bufs.add(result);
197
}
198
bufs.add(buf);
199
}
200
}
201
// if the last call to read returned -1 or the number of bytes
202
// requested have been read then break
203
} while (n >= 0 && remaining > 0);
204
205
if (bufs == null) {
206
if (result == null) {
207
return new byte[0];
208
}
209
return result.length == total ?
210
result : Arrays.copyOf(result, total);
211
}
212
213
result = new byte[total];
214
int offset = 0;
215
remaining = total;
216
for (byte[] b : bufs) {
217
int count = Math.min(b.length, remaining);
218
System.arraycopy(b, 0, result, offset, count);
219
offset += count;
220
remaining -= count;
221
}
222
223
return result;
224
}
225
226
/**
227
* Reads the requested number of bytes from the input stream into the given
228
* byte array. This method blocks until {@code len} bytes of input data have
229
* been read, end of stream is detected, or an exception is thrown. The
230
* number of bytes actually read, possibly zero, is returned. This method
231
* does not close the input stream.
232
*
233
* <p> In the case where end of stream is reached before {@code len} bytes
234
* have been read, then the actual number of bytes read will be returned.
235
* When this stream reaches end of stream, further invocations of this
236
* method will return zero.
237
*
238
* <p> If {@code len} is zero, then no bytes are read and {@code 0} is
239
* returned; otherwise, there is an attempt to read up to {@code len} bytes.
240
*
241
* <p> The first byte read is stored into element {@code b[off]}, the next
242
* one in to {@code b[off+1]}, and so on. The number of bytes read is, at
243
* most, equal to {@code len}. Let <i>k</i> be the number of bytes actually
244
* read; these bytes will be stored in elements {@code b[off]} through
245
* {@code b[off+}<i>k</i>{@code -1]}, leaving elements {@code b[off+}<i>k</i>
246
* {@code ]} through {@code b[off+len-1]} unaffected.
247
*
248
* <p> The behavior for the case where the input stream is <i>asynchronously
249
* closed</i>, or the thread interrupted during the read, is highly input
250
* stream specific, and therefore not specified.
251
*
252
* <p> If an I/O error occurs reading from the input stream, then it may do
253
* so after some, but not all, bytes of {@code b} have been updated with
254
* data from the input stream. Consequently the input stream and {@code b}
255
* may be in an inconsistent state. It is strongly recommended that the
256
* stream be promptly closed if an I/O error occurs.
257
*
258
* @param is input stream, must not be null
259
* @param b the byte array into which the data is read
260
* @param off the start offset in {@code b} at which the data is written
261
* @param len the maximum number of bytes to read
262
* @return the actual number of bytes read into the buffer
263
* @throws IOException if an I/O error occurs
264
* @throws NullPointerException if {@code b} is {@code null}
265
* @throws IndexOutOfBoundsException If {@code off} is negative, {@code len}
266
* is negative, or {@code len} is greater than {@code b.length - off}
267
*
268
* @since 1.9
269
*/
270
public static int readNBytes(InputStream is, byte[] b, int off, int len) throws IOException {
271
Objects.requireNonNull(b);
272
if (off < 0 || len < 0 || len > b.length - off)
273
throw new IndexOutOfBoundsException();
274
int n = 0;
275
while (n < len) {
276
int count = is.read(b, off + n, len - n);
277
if (count < 0)
278
break;
279
n += count;
280
}
281
return n;
282
}
283
284
/**
285
* Compatibility wrapper for third party users of
286
* {@code sun.misc.IOUtils.readFully} following its
287
* removal in JDK-8231139.
288
*
289
* Read up to {@code length} of bytes from {@code in}
290
* until EOF is detected.
291
*
292
* @param is input stream, must not be null
293
* @param length number of bytes to read
294
* @param readAll if true, an EOFException will be thrown if not enough
295
* bytes are read.
296
* @return bytes read
297
* @throws EOFException if there are not enough bytes in the stream
298
* @throws IOException if an I/O error occurs or {@code length} is negative
299
* @throws OutOfMemoryError if an array of the required size cannot be
300
* allocated.
301
*/
302
public static byte[] readFully(InputStream is, int length, boolean readAll)
303
throws IOException {
304
if (length < 0) {
305
throw new IOException("length cannot be negative: " + length);
306
}
307
if (readAll) {
308
return readExactlyNBytes(is, length);
309
} else {
310
return readNBytes(is, length);
311
}
312
}
313
}
314
315