2 * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
28 import org.apidesign.bck2brwsr.emul.lang.System;
31 * A <code>ByteArrayInputStream</code> contains
32 * an internal buffer that contains bytes that
33 * may be read from the stream. An internal
34 * counter keeps track of the next byte to
35 * be supplied by the <code>read</code> method.
37 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
38 * this class can be called after the stream has been closed without
39 * generating an <tt>IOException</tt>.
41 * @author Arthur van Hoff
42 * @see java.io.StringBufferInputStream
46 class ByteArrayInputStream extends InputStream {
49 * An array of bytes that was provided
50 * by the creator of the stream. Elements <code>buf[0]</code>
51 * through <code>buf[count-1]</code> are the
52 * only bytes that can ever be read from the
53 * stream; element <code>buf[pos]</code> is
54 * the next byte to be read.
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</code>.
62 * The next byte to be read from the input stream buffer
63 * will be <code>buf[pos]</code>.
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()</code> method.
72 * The current buffer position is set to this point by the
73 * <code>reset()</code> method.
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).
80 protected int mark = 0;
83 * The index one greater than the last valid character in the input
85 * This value should always be nonnegative
86 * and not larger than the length of <code>buf</code>.
87 * It is one greater than the position of
88 * the last byte within <code>buf</code> that
89 * can ever be read from the input stream buffer.
94 * Creates a <code>ByteArrayInputStream</code>
95 * so that it uses <code>buf</code> as its
97 * The buffer array is not copied.
98 * The initial value of <code>pos</code>
99 * is <code>0</code> and the initial value
100 * of <code>count</code> is the length of
103 * @param buf the input buffer.
105 public ByteArrayInputStream(byte buf[]) {
108 this.count = buf.length;
112 * Creates <code>ByteArrayInputStream</code>
113 * that uses <code>buf</code> as its
114 * buffer array. The initial value of <code>pos</code>
115 * is <code>offset</code> and the initial value
116 * of <code>count</code> is the minimum of <code>offset+length</code>
117 * and <code>buf.length</code>.
118 * The buffer array is not copied. The buffer's mark is
119 * set to the specified offset.
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.
125 public ByteArrayInputStream(byte buf[], int offset, int length) {
128 this.count = Math.min(offset + length, buf.length);
133 * Reads the next byte of data from this input stream. The value
134 * byte is returned as an <code>int</code> in the range
135 * <code>0</code> to <code>255</code>. If no byte is available
136 * because the end of the stream has been reached, the value
137 * <code>-1</code> is returned.
139 * This <code>read</code> method
142 * @return the next byte of data, or <code>-1</code> if the end of the
143 * stream has been reached.
145 public synchronized int read() {
146 return (pos < count) ? (buf[pos++] & 0xff) : -1;
150 * Reads up to <code>len</code> bytes of data into an array of bytes
151 * from this input stream.
152 * If <code>pos</code> equals <code>count</code>,
153 * then <code>-1</code> is returned to indicate
154 * end of file. Otherwise, the number <code>k</code>
155 * of bytes read is equal to the smaller of
156 * <code>len</code> and <code>count-pos</code>.
157 * If <code>k</code> is positive, then bytes
158 * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
159 * are copied into <code>b[off]</code> through
160 * <code>b[off+k-1]</code> in the manner performed
161 * by <code>System.arraycopy</code>. The
162 * value <code>k</code> is added into <code>pos</code>
163 * and <code>k</code> is returned.
165 * This <code>read</code> method cannot block.
167 * @param b the buffer into which the data is read.
168 * @param off the start offset in the destination array <code>b</code>
169 * @param len the maximum number of bytes read.
170 * @return the total number of bytes read into the buffer, or
171 * <code>-1</code> if there is no more data because the end of
172 * the stream has been reached.
173 * @exception NullPointerException If <code>b</code> is <code>null</code>.
174 * @exception IndexOutOfBoundsException If <code>off</code> is negative,
175 * <code>len</code> is negative, or <code>len</code> is greater than
176 * <code>b.length - off</code>
178 public synchronized int read(byte b[], int off, int len) {
180 throw new NullPointerException();
181 } else if (off < 0 || len < 0 || len > b.length - off) {
182 throw new IndexOutOfBoundsException();
189 int avail = count - pos;
196 System.arraycopy(buf, pos, b, off, len);
202 * Skips <code>n</code> bytes of input from this input stream. Fewer
203 * bytes might be skipped if the end of the input stream is reached.
204 * The actual number <code>k</code>
205 * of bytes to be skipped is equal to the smaller
206 * of <code>n</code> and <code>count-pos</code>.
207 * The value <code>k</code> is added into <code>pos</code>
208 * and <code>k</code> is returned.
210 * @param n the number of bytes to be skipped.
211 * @return the actual number of bytes skipped.
213 public synchronized long skip(long n) {
214 long k = count - pos;
224 * Returns the number of remaining bytes that can be read (or skipped over)
225 * from this input stream.
227 * The value returned is <code>count - pos</code>,
228 * which is the number of bytes remaining to be read from the input buffer.
230 * @return the number of remaining bytes that can be read (or skipped
231 * over) from this input stream without blocking.
233 public synchronized int available() {
238 * Tests if this <code>InputStream</code> supports mark/reset. The
239 * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
240 * always returns <code>true</code>.
244 public boolean markSupported() {
249 * Set the current marked position in the stream.
250 * ByteArrayInputStream objects are marked at position zero by
251 * default when constructed. They may be marked at another
252 * position within the buffer by this method.
254 * If no mark has been set, then the value of the mark is the
255 * offset passed to the constructor (or 0 if the offset was not
258 * <p> Note: The <code>readAheadLimit</code> for this class
263 public void mark(int readAheadLimit) {
268 * Resets the buffer to the marked position. The marked position
269 * is 0 unless another position was marked or an offset was specified
270 * in the constructor.
272 public synchronized void reset() {
277 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
278 * this class can be called after the stream has been closed without
279 * generating an <tt>IOException</tt>.
282 public void close() throws IOException {