Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/java/io/ByteArrayInputStream.java
67794 views
1
/*
2
* Copyright (c) 1994, 2021, 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
package java.io;
27
28
import java.util.Arrays;
29
import java.util.Objects;
30
31
/**
32
* A {@code ByteArrayInputStream} contains
33
* an internal buffer that contains bytes that
34
* may be read from the stream. An internal
35
* counter keeps track of the next byte to
36
* be supplied by the {@code read} method.
37
* <p>
38
* Closing a {@code ByteArrayInputStream} has no effect. The methods in
39
* this class can be called after the stream has been closed without
40
* generating an {@code IOException}.
41
*
42
* @author Arthur van Hoff
43
* @see java.io.StringBufferInputStream
44
* @since 1.0
45
*/
46
public class ByteArrayInputStream extends InputStream {
47
48
/**
49
* An array of bytes that was provided
50
* by the creator of the stream. Elements {@code buf[0]}
51
* through {@code buf[count-1]} are the
52
* only bytes that can ever be read from the
53
* stream; element {@code buf[pos]} is
54
* the next byte to be read.
55
*/
56
protected byte buf[];
57
58
/**
59
* The index of the next character to read from the input stream buffer.
60
* This value should always be nonnegative
61
* and not larger than the value of {@code count}.
62
* The next byte to be read from the input stream buffer
63
* will be {@code buf[pos]}.
64
*/
65
protected int pos;
66
67
/**
68
* The currently marked position in the stream.
69
* ByteArrayInputStream objects are marked at position zero by
70
* default when constructed. They may be marked at another
71
* position within the buffer by the {@code mark()} method.
72
* The current buffer position is set to this point by the
73
* {@code reset()} method.
74
* <p>
75
* If no mark has been set, then the value of mark is the offset
76
* passed to the constructor (or 0 if the offset was not supplied).
77
*
78
* @since 1.1
79
*/
80
protected int mark = 0;
81
82
/**
83
* The index one greater than the last valid character in the input
84
* stream buffer.
85
* This value should always be nonnegative
86
* and not larger than the length of {@code buf}.
87
* It is one greater than the position of
88
* the last byte within {@code buf} that
89
* can ever be read from the input stream buffer.
90
*/
91
protected int count;
92
93
/**
94
* Creates a {@code ByteArrayInputStream}
95
* so that it uses {@code buf} as its
96
* buffer array.
97
* The buffer array is not copied.
98
* The initial value of {@code pos}
99
* is {@code 0} and the initial value
100
* of {@code count} is the length of
101
* {@code buf}.
102
*
103
* @param buf the input buffer.
104
*/
105
public ByteArrayInputStream(byte buf[]) {
106
this.buf = buf;
107
this.pos = 0;
108
this.count = buf.length;
109
}
110
111
/**
112
* Creates {@code ByteArrayInputStream}
113
* that uses {@code buf} as its
114
* buffer array. The initial value of {@code pos}
115
* is {@code offset} and the initial value
116
* of {@code count} is the minimum of {@code offset+length}
117
* and {@code buf.length}.
118
* The buffer array is not copied. The buffer's mark is
119
* set to the specified offset.
120
*
121
* @param buf the input buffer.
122
* @param offset the offset in the buffer of the first byte to read.
123
* @param length the maximum number of bytes to read from the buffer.
124
*/
125
public ByteArrayInputStream(byte buf[], int offset, int length) {
126
this.buf = buf;
127
this.pos = offset;
128
this.count = Math.min(offset + length, buf.length);
129
this.mark = offset;
130
}
131
132
/**
133
* Reads the next byte of data from this input stream. The value
134
* byte is returned as an {@code int} in the range
135
* {@code 0} to {@code 255}. If no byte is available
136
* because the end of the stream has been reached, the value
137
* {@code -1} is returned.
138
* <p>
139
* This {@code read} method
140
* cannot block.
141
*
142
* @return the next byte of data, or {@code -1} if the end of the
143
* stream has been reached.
144
*/
145
public synchronized int read() {
146
return (pos < count) ? (buf[pos++] & 0xff) : -1;
147
}
148
149
/**
150
* Reads up to {@code len} bytes of data into an array of bytes from this
151
* input stream. If {@code pos} equals {@code count}, then {@code -1} is
152
* returned to indicate end of file. Otherwise, the number {@code k} of
153
* bytes read is equal to the smaller of {@code len} and {@code count-pos}.
154
* If {@code k} is positive, then bytes {@code buf[pos]} through
155
* {@code buf[pos+k-1]} are copied into {@code b[off]} through
156
* {@code b[off+k-1]} in the manner performed by {@code System.arraycopy}.
157
* The value {@code k} is added into {@code pos} and {@code k} is returned.
158
* <p>
159
* Unlike the {@link InputStream#read(byte[],int,int) overridden method}
160
* of {@code InputStream}, this method returns {@code -1} instead of zero
161
* if the end of the stream has been reached and {@code len == 0}.
162
* <p>
163
* This {@code read} method cannot block.
164
*
165
* @param b the buffer into which the data is read.
166
* @param off the start offset in the destination array {@code b}
167
* @param len the maximum number of bytes read.
168
* @return the total number of bytes read into the buffer, or
169
* {@code -1} if there is no more data because the end of
170
* the stream has been reached.
171
* @throws NullPointerException If {@code b} is {@code null}.
172
* @throws IndexOutOfBoundsException If {@code off} is negative,
173
* {@code len} is negative, or {@code len} is greater than
174
* {@code b.length - off}
175
*/
176
public synchronized int read(byte b[], int off, int len) {
177
Objects.checkFromIndexSize(off, len, b.length);
178
179
if (pos >= count) {
180
return -1;
181
}
182
183
int avail = count - pos;
184
if (len > avail) {
185
len = avail;
186
}
187
if (len <= 0) {
188
return 0;
189
}
190
System.arraycopy(buf, pos, b, off, len);
191
pos += len;
192
return len;
193
}
194
195
public synchronized byte[] readAllBytes() {
196
byte[] result = Arrays.copyOfRange(buf, pos, count);
197
pos = count;
198
return result;
199
}
200
201
public int readNBytes(byte[] b, int off, int len) {
202
int n = read(b, off, len);
203
return n == -1 ? 0 : n;
204
}
205
206
public synchronized long transferTo(OutputStream out) throws IOException {
207
int len = count - pos;
208
out.write(buf, pos, len);
209
pos = count;
210
return len;
211
}
212
213
/**
214
* Skips {@code n} bytes of input from this input stream. Fewer
215
* bytes might be skipped if the end of the input stream is reached.
216
* The actual number {@code k}
217
* of bytes to be skipped is equal to the smaller
218
* of {@code n} and {@code count-pos}.
219
* The value {@code k} is added into {@code pos}
220
* and {@code k} is returned.
221
*
222
* @param n the number of bytes to be skipped.
223
* @return the actual number of bytes skipped.
224
*/
225
public synchronized long skip(long n) {
226
long k = count - pos;
227
if (n < k) {
228
k = n < 0 ? 0 : n;
229
}
230
231
pos += k;
232
return k;
233
}
234
235
/**
236
* Returns the number of remaining bytes that can be read (or skipped over)
237
* from this input stream.
238
* <p>
239
* The value returned is {@code count - pos},
240
* which is the number of bytes remaining to be read from the input buffer.
241
*
242
* @return the number of remaining bytes that can be read (or skipped
243
* over) from this input stream without blocking.
244
*/
245
public synchronized int available() {
246
return count - pos;
247
}
248
249
/**
250
* Tests if this {@code InputStream} supports mark/reset. The
251
* {@code markSupported} method of {@code ByteArrayInputStream}
252
* always returns {@code true}.
253
*
254
* @since 1.1
255
*/
256
public boolean markSupported() {
257
return true;
258
}
259
260
/**
261
* Set the current marked position in the stream.
262
* ByteArrayInputStream objects are marked at position zero by
263
* default when constructed. They may be marked at another
264
* position within the buffer by this method.
265
* <p>
266
* If no mark has been set, then the value of the mark is the
267
* offset passed to the constructor (or 0 if the offset was not
268
* supplied).
269
*
270
* <p> Note: The {@code readAheadLimit} for this class
271
* has no meaning.
272
*
273
* @since 1.1
274
*/
275
public void mark(int readAheadLimit) {
276
mark = pos;
277
}
278
279
/**
280
* Resets the buffer to the marked position. The marked position
281
* is 0 unless another position was marked or an offset was specified
282
* in the constructor.
283
*/
284
public synchronized void reset() {
285
pos = mark;
286
}
287
288
/**
289
* Closing a {@code ByteArrayInputStream} has no effect. The methods in
290
* this class can be called after the stream has been closed without
291
* generating an {@code IOException}.
292
*/
293
public void close() throws IOException {
294
}
295
296
}
297
298