jaroslav@163
|
1 |
/*
|
jaroslav@163
|
2 |
* Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
|
jaroslav@163
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
jaroslav@163
|
4 |
*
|
jaroslav@163
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
jaroslav@163
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
jaroslav@163
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
jaroslav@163
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
jaroslav@163
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
jaroslav@163
|
10 |
*
|
jaroslav@163
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
jaroslav@163
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
jaroslav@163
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
jaroslav@163
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
jaroslav@163
|
15 |
* accompanied this code).
|
jaroslav@163
|
16 |
*
|
jaroslav@163
|
17 |
* You should have received a copy of the GNU General Public License version
|
jaroslav@163
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
jaroslav@163
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
jaroslav@163
|
20 |
*
|
jaroslav@163
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
jaroslav@163
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
jaroslav@163
|
23 |
* questions.
|
jaroslav@163
|
24 |
*/
|
jaroslav@163
|
25 |
|
jaroslav@163
|
26 |
package java.io;
|
jaroslav@163
|
27 |
|
jaroslav@560
|
28 |
import org.apidesign.bck2brwsr.emul.lang.System;
|
jaroslav@560
|
29 |
|
jaroslav@163
|
30 |
/**
|
jaroslav@163
|
31 |
* A <code>ByteArrayInputStream</code> contains
|
jaroslav@163
|
32 |
* an internal buffer that contains bytes that
|
jaroslav@163
|
33 |
* may be read from the stream. An internal
|
jaroslav@163
|
34 |
* counter keeps track of the next byte to
|
jaroslav@163
|
35 |
* be supplied by the <code>read</code> method.
|
jaroslav@163
|
36 |
* <p>
|
jaroslav@163
|
37 |
* Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
|
jaroslav@163
|
38 |
* this class can be called after the stream has been closed without
|
jaroslav@163
|
39 |
* generating an <tt>IOException</tt>.
|
jaroslav@163
|
40 |
*
|
jaroslav@163
|
41 |
* @author Arthur van Hoff
|
jaroslav@163
|
42 |
* @see java.io.StringBufferInputStream
|
jaroslav@163
|
43 |
* @since JDK1.0
|
jaroslav@163
|
44 |
*/
|
jaroslav@163
|
45 |
public
|
jaroslav@163
|
46 |
class ByteArrayInputStream extends InputStream {
|
jaroslav@163
|
47 |
|
jaroslav@163
|
48 |
/**
|
jaroslav@163
|
49 |
* An array of bytes that was provided
|
jaroslav@163
|
50 |
* by the creator of the stream. Elements <code>buf[0]</code>
|
jaroslav@163
|
51 |
* through <code>buf[count-1]</code> are the
|
jaroslav@163
|
52 |
* only bytes that can ever be read from the
|
jaroslav@163
|
53 |
* stream; element <code>buf[pos]</code> is
|
jaroslav@163
|
54 |
* the next byte to be read.
|
jaroslav@163
|
55 |
*/
|
jaroslav@163
|
56 |
protected byte buf[];
|
jaroslav@163
|
57 |
|
jaroslav@163
|
58 |
/**
|
jaroslav@163
|
59 |
* The index of the next character to read from the input stream buffer.
|
jaroslav@163
|
60 |
* This value should always be nonnegative
|
jaroslav@163
|
61 |
* and not larger than the value of <code>count</code>.
|
jaroslav@163
|
62 |
* The next byte to be read from the input stream buffer
|
jaroslav@163
|
63 |
* will be <code>buf[pos]</code>.
|
jaroslav@163
|
64 |
*/
|
jaroslav@163
|
65 |
protected int pos;
|
jaroslav@163
|
66 |
|
jaroslav@163
|
67 |
/**
|
jaroslav@163
|
68 |
* The currently marked position in the stream.
|
jaroslav@163
|
69 |
* ByteArrayInputStream objects are marked at position zero by
|
jaroslav@163
|
70 |
* default when constructed. They may be marked at another
|
jaroslav@163
|
71 |
* position within the buffer by the <code>mark()</code> method.
|
jaroslav@163
|
72 |
* The current buffer position is set to this point by the
|
jaroslav@163
|
73 |
* <code>reset()</code> method.
|
jaroslav@163
|
74 |
* <p>
|
jaroslav@163
|
75 |
* If no mark has been set, then the value of mark is the offset
|
jaroslav@163
|
76 |
* passed to the constructor (or 0 if the offset was not supplied).
|
jaroslav@163
|
77 |
*
|
jaroslav@163
|
78 |
* @since JDK1.1
|
jaroslav@163
|
79 |
*/
|
jaroslav@163
|
80 |
protected int mark = 0;
|
jaroslav@163
|
81 |
|
jaroslav@163
|
82 |
/**
|
jaroslav@163
|
83 |
* The index one greater than the last valid character in the input
|
jaroslav@163
|
84 |
* stream buffer.
|
jaroslav@163
|
85 |
* This value should always be nonnegative
|
jaroslav@163
|
86 |
* and not larger than the length of <code>buf</code>.
|
jaroslav@163
|
87 |
* It is one greater than the position of
|
jaroslav@163
|
88 |
* the last byte within <code>buf</code> that
|
jaroslav@163
|
89 |
* can ever be read from the input stream buffer.
|
jaroslav@163
|
90 |
*/
|
jaroslav@163
|
91 |
protected int count;
|
jaroslav@163
|
92 |
|
jaroslav@163
|
93 |
/**
|
jaroslav@163
|
94 |
* Creates a <code>ByteArrayInputStream</code>
|
jaroslav@163
|
95 |
* so that it uses <code>buf</code> as its
|
jaroslav@163
|
96 |
* buffer array.
|
jaroslav@163
|
97 |
* The buffer array is not copied.
|
jaroslav@163
|
98 |
* The initial value of <code>pos</code>
|
jaroslav@163
|
99 |
* is <code>0</code> and the initial value
|
jaroslav@163
|
100 |
* of <code>count</code> is the length of
|
jaroslav@163
|
101 |
* <code>buf</code>.
|
jaroslav@163
|
102 |
*
|
jaroslav@163
|
103 |
* @param buf the input buffer.
|
jaroslav@163
|
104 |
*/
|
jaroslav@163
|
105 |
public ByteArrayInputStream(byte buf[]) {
|
jaroslav@163
|
106 |
this.buf = buf;
|
jaroslav@163
|
107 |
this.pos = 0;
|
jaroslav@163
|
108 |
this.count = buf.length;
|
jaroslav@163
|
109 |
}
|
jaroslav@163
|
110 |
|
jaroslav@163
|
111 |
/**
|
jaroslav@163
|
112 |
* Creates <code>ByteArrayInputStream</code>
|
jaroslav@163
|
113 |
* that uses <code>buf</code> as its
|
jaroslav@163
|
114 |
* buffer array. The initial value of <code>pos</code>
|
jaroslav@163
|
115 |
* is <code>offset</code> and the initial value
|
jaroslav@163
|
116 |
* of <code>count</code> is the minimum of <code>offset+length</code>
|
jaroslav@163
|
117 |
* and <code>buf.length</code>.
|
jaroslav@163
|
118 |
* The buffer array is not copied. The buffer's mark is
|
jaroslav@163
|
119 |
* set to the specified offset.
|
jaroslav@163
|
120 |
*
|
jaroslav@163
|
121 |
* @param buf the input buffer.
|
jaroslav@163
|
122 |
* @param offset the offset in the buffer of the first byte to read.
|
jaroslav@163
|
123 |
* @param length the maximum number of bytes to read from the buffer.
|
jaroslav@163
|
124 |
*/
|
jaroslav@163
|
125 |
public ByteArrayInputStream(byte buf[], int offset, int length) {
|
jaroslav@163
|
126 |
this.buf = buf;
|
jaroslav@163
|
127 |
this.pos = offset;
|
jaroslav@163
|
128 |
this.count = Math.min(offset + length, buf.length);
|
jaroslav@163
|
129 |
this.mark = offset;
|
jaroslav@163
|
130 |
}
|
jaroslav@163
|
131 |
|
jaroslav@163
|
132 |
/**
|
jaroslav@163
|
133 |
* Reads the next byte of data from this input stream. The value
|
jaroslav@163
|
134 |
* byte is returned as an <code>int</code> in the range
|
jaroslav@163
|
135 |
* <code>0</code> to <code>255</code>. If no byte is available
|
jaroslav@163
|
136 |
* because the end of the stream has been reached, the value
|
jaroslav@163
|
137 |
* <code>-1</code> is returned.
|
jaroslav@163
|
138 |
* <p>
|
jaroslav@163
|
139 |
* This <code>read</code> method
|
jaroslav@163
|
140 |
* cannot block.
|
jaroslav@163
|
141 |
*
|
jaroslav@163
|
142 |
* @return the next byte of data, or <code>-1</code> if the end of the
|
jaroslav@163
|
143 |
* stream has been reached.
|
jaroslav@163
|
144 |
*/
|
jaroslav@163
|
145 |
public synchronized int read() {
|
jaroslav@163
|
146 |
return (pos < count) ? (buf[pos++] & 0xff) : -1;
|
jaroslav@163
|
147 |
}
|
jaroslav@163
|
148 |
|
jaroslav@163
|
149 |
/**
|
jaroslav@163
|
150 |
* Reads up to <code>len</code> bytes of data into an array of bytes
|
jaroslav@163
|
151 |
* from this input stream.
|
jaroslav@163
|
152 |
* If <code>pos</code> equals <code>count</code>,
|
jaroslav@163
|
153 |
* then <code>-1</code> is returned to indicate
|
jaroslav@163
|
154 |
* end of file. Otherwise, the number <code>k</code>
|
jaroslav@163
|
155 |
* of bytes read is equal to the smaller of
|
jaroslav@163
|
156 |
* <code>len</code> and <code>count-pos</code>.
|
jaroslav@163
|
157 |
* If <code>k</code> is positive, then bytes
|
jaroslav@163
|
158 |
* <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
|
jaroslav@163
|
159 |
* are copied into <code>b[off]</code> through
|
jaroslav@163
|
160 |
* <code>b[off+k-1]</code> in the manner performed
|
jaroslav@163
|
161 |
* by <code>System.arraycopy</code>. The
|
jaroslav@163
|
162 |
* value <code>k</code> is added into <code>pos</code>
|
jaroslav@163
|
163 |
* and <code>k</code> is returned.
|
jaroslav@163
|
164 |
* <p>
|
jaroslav@163
|
165 |
* This <code>read</code> method cannot block.
|
jaroslav@163
|
166 |
*
|
jaroslav@163
|
167 |
* @param b the buffer into which the data is read.
|
jaroslav@163
|
168 |
* @param off the start offset in the destination array <code>b</code>
|
jaroslav@163
|
169 |
* @param len the maximum number of bytes read.
|
jaroslav@163
|
170 |
* @return the total number of bytes read into the buffer, or
|
jaroslav@163
|
171 |
* <code>-1</code> if there is no more data because the end of
|
jaroslav@163
|
172 |
* the stream has been reached.
|
jaroslav@163
|
173 |
* @exception NullPointerException If <code>b</code> is <code>null</code>.
|
jaroslav@163
|
174 |
* @exception IndexOutOfBoundsException If <code>off</code> is negative,
|
jaroslav@163
|
175 |
* <code>len</code> is negative, or <code>len</code> is greater than
|
jaroslav@163
|
176 |
* <code>b.length - off</code>
|
jaroslav@163
|
177 |
*/
|
jaroslav@163
|
178 |
public synchronized int read(byte b[], int off, int len) {
|
jaroslav@163
|
179 |
if (b == null) {
|
jaroslav@163
|
180 |
throw new NullPointerException();
|
jaroslav@163
|
181 |
} else if (off < 0 || len < 0 || len > b.length - off) {
|
jaroslav@163
|
182 |
throw new IndexOutOfBoundsException();
|
jaroslav@163
|
183 |
}
|
jaroslav@163
|
184 |
|
jaroslav@163
|
185 |
if (pos >= count) {
|
jaroslav@163
|
186 |
return -1;
|
jaroslav@163
|
187 |
}
|
jaroslav@163
|
188 |
|
jaroslav@163
|
189 |
int avail = count - pos;
|
jaroslav@163
|
190 |
if (len > avail) {
|
jaroslav@163
|
191 |
len = avail;
|
jaroslav@163
|
192 |
}
|
jaroslav@163
|
193 |
if (len <= 0) {
|
jaroslav@163
|
194 |
return 0;
|
jaroslav@163
|
195 |
}
|
jaroslav@560
|
196 |
System.arraycopy(buf, pos, b, off, len);
|
jaroslav@163
|
197 |
pos += len;
|
jaroslav@163
|
198 |
return len;
|
jaroslav@163
|
199 |
}
|
jaroslav@163
|
200 |
|
jaroslav@163
|
201 |
/**
|
jaroslav@163
|
202 |
* Skips <code>n</code> bytes of input from this input stream. Fewer
|
jaroslav@163
|
203 |
* bytes might be skipped if the end of the input stream is reached.
|
jaroslav@163
|
204 |
* The actual number <code>k</code>
|
jaroslav@163
|
205 |
* of bytes to be skipped is equal to the smaller
|
jaroslav@163
|
206 |
* of <code>n</code> and <code>count-pos</code>.
|
jaroslav@163
|
207 |
* The value <code>k</code> is added into <code>pos</code>
|
jaroslav@163
|
208 |
* and <code>k</code> is returned.
|
jaroslav@163
|
209 |
*
|
jaroslav@163
|
210 |
* @param n the number of bytes to be skipped.
|
jaroslav@163
|
211 |
* @return the actual number of bytes skipped.
|
jaroslav@163
|
212 |
*/
|
jaroslav@163
|
213 |
public synchronized long skip(long n) {
|
jaroslav@163
|
214 |
long k = count - pos;
|
jaroslav@163
|
215 |
if (n < k) {
|
jaroslav@163
|
216 |
k = n < 0 ? 0 : n;
|
jaroslav@163
|
217 |
}
|
jaroslav@163
|
218 |
|
jaroslav@163
|
219 |
pos += k;
|
jaroslav@163
|
220 |
return k;
|
jaroslav@163
|
221 |
}
|
jaroslav@163
|
222 |
|
jaroslav@163
|
223 |
/**
|
jaroslav@163
|
224 |
* Returns the number of remaining bytes that can be read (or skipped over)
|
jaroslav@163
|
225 |
* from this input stream.
|
jaroslav@163
|
226 |
* <p>
|
jaroslav@163
|
227 |
* The value returned is <code>count - pos</code>,
|
jaroslav@163
|
228 |
* which is the number of bytes remaining to be read from the input buffer.
|
jaroslav@163
|
229 |
*
|
jaroslav@163
|
230 |
* @return the number of remaining bytes that can be read (or skipped
|
jaroslav@163
|
231 |
* over) from this input stream without blocking.
|
jaroslav@163
|
232 |
*/
|
jaroslav@163
|
233 |
public synchronized int available() {
|
jaroslav@163
|
234 |
return count - pos;
|
jaroslav@163
|
235 |
}
|
jaroslav@163
|
236 |
|
jaroslav@163
|
237 |
/**
|
jaroslav@163
|
238 |
* Tests if this <code>InputStream</code> supports mark/reset. The
|
jaroslav@163
|
239 |
* <code>markSupported</code> method of <code>ByteArrayInputStream</code>
|
jaroslav@163
|
240 |
* always returns <code>true</code>.
|
jaroslav@163
|
241 |
*
|
jaroslav@163
|
242 |
* @since JDK1.1
|
jaroslav@163
|
243 |
*/
|
jaroslav@163
|
244 |
public boolean markSupported() {
|
jaroslav@163
|
245 |
return true;
|
jaroslav@163
|
246 |
}
|
jaroslav@163
|
247 |
|
jaroslav@163
|
248 |
/**
|
jaroslav@163
|
249 |
* Set the current marked position in the stream.
|
jaroslav@163
|
250 |
* ByteArrayInputStream objects are marked at position zero by
|
jaroslav@163
|
251 |
* default when constructed. They may be marked at another
|
jaroslav@163
|
252 |
* position within the buffer by this method.
|
jaroslav@163
|
253 |
* <p>
|
jaroslav@163
|
254 |
* If no mark has been set, then the value of the mark is the
|
jaroslav@163
|
255 |
* offset passed to the constructor (or 0 if the offset was not
|
jaroslav@163
|
256 |
* supplied).
|
jaroslav@163
|
257 |
*
|
jaroslav@163
|
258 |
* <p> Note: The <code>readAheadLimit</code> for this class
|
jaroslav@163
|
259 |
* has no meaning.
|
jaroslav@163
|
260 |
*
|
jaroslav@163
|
261 |
* @since JDK1.1
|
jaroslav@163
|
262 |
*/
|
jaroslav@163
|
263 |
public void mark(int readAheadLimit) {
|
jaroslav@163
|
264 |
mark = pos;
|
jaroslav@163
|
265 |
}
|
jaroslav@163
|
266 |
|
jaroslav@163
|
267 |
/**
|
jaroslav@163
|
268 |
* Resets the buffer to the marked position. The marked position
|
jaroslav@163
|
269 |
* is 0 unless another position was marked or an offset was specified
|
jaroslav@163
|
270 |
* in the constructor.
|
jaroslav@163
|
271 |
*/
|
jaroslav@163
|
272 |
public synchronized void reset() {
|
jaroslav@163
|
273 |
pos = mark;
|
jaroslav@163
|
274 |
}
|
jaroslav@163
|
275 |
|
jaroslav@163
|
276 |
/**
|
jaroslav@163
|
277 |
* Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
|
jaroslav@163
|
278 |
* this class can be called after the stream has been closed without
|
jaroslav@163
|
279 |
* generating an <tt>IOException</tt>.
|
jaroslav@163
|
280 |
* <p>
|
jaroslav@163
|
281 |
*/
|
jaroslav@163
|
282 |
public void close() throws IOException {
|
jaroslav@163
|
283 |
}
|
jaroslav@163
|
284 |
|
jaroslav@163
|
285 |
}
|