1 /* ByteArrayInputStream.java -- Read an array as a stream
2 Copyright (C) 1998, 1999, 2001, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
42 * This class permits an array of bytes to be read as an input stream.
44 * @author Warren Levy (warrenl@cygnus.com)
45 * @author Aaron M. Renn (arenn@urbanophile.com)
48 public class ByteArrayInputStream extends InputStream
51 * The array that contains the data supplied during read operations
56 * The array index of the next byte to be read from the buffer
62 * The currently marked position in the stream. This defaults to 0, so a
63 * reset operation on the stream resets it to read from array index 0 in
64 * the buffer - even if the stream was initially created with an offset
70 * This indicates the maximum number of bytes that can be read from this
71 * stream. It is the array index of the position after the last valid
72 * byte in the buffer <code>buf</code>
77 * Create a new ByteArrayInputStream that will read bytes from the passed
78 * in byte array. This stream will read from the beginning to the end
79 * of the array. It is identical to calling an overloaded constructor
80 * as <code>ByteArrayInputStream(buf, 0, buf.length)</code>.
82 * Note that this array is not copied. If its contents are changed
83 * while this stream is being read, those changes will be reflected in the
84 * bytes supplied to the reader. Please use caution in changing the
85 * contents of the buffer while this stream is open.
87 * @param buffer The byte array buffer this stream will read from.
89 public ByteArrayInputStream(byte[] buffer)
91 this(buffer, 0, buffer.length);
95 * Create a new ByteArrayInputStream that will read bytes from the
96 * passed in byte array. This stream will read from position
97 * <code>offset</code> in the array for a length of
98 * <code>length</code> bytes past <code>offset</code>. If the
99 * stream is reset to a position before <code>offset</code> then
100 * more than <code>length</code> bytes can be read from the stream.
101 * The <code>length</code> value should be viewed as the array index
102 * one greater than the last position in the buffer to read.
104 * Note that this array is not copied. If its contents are changed
105 * while this stream is being read, those changes will be reflected in the
106 * bytes supplied to the reader. Please use caution in changing the
107 * contents of the buffer while this stream is open.
109 * @param buffer The byte array buffer this stream will read from.
110 * @param offset The index into the buffer to start reading bytes from
111 * @param length The number of bytes to read from the buffer
113 public ByteArrayInputStream(byte[] buffer, int offset, int length)
115 if (offset < 0 || length < 0 || offset > buffer.length)
116 throw new IllegalArgumentException();
120 count = offset + length;
121 if (count > buf.length)
129 * This method returns the number of bytes available to be read from this
130 * stream. The value returned will be equal to <code>count - pos</code>.
132 * @return The number of bytes that can be read from this stream
133 * before blocking, which is all of them
135 public synchronized int available()
141 * This method sets the mark position in this stream to the current
142 * position. Note that the <code>readlimit</code> parameter in this
143 * method does nothing as this stream is always capable of
144 * remembering all the bytes int it.
146 * Note that in this class the mark position is set by default to
147 * position 0 in the stream. This is in constrast to some other
148 * stream types where there is no default mark position.
150 * @param readLimit The number of bytes this stream must remember.
151 * This parameter is ignored.
153 public synchronized void mark(int readLimit)
155 // readLimit is ignored per Java Class Lib. book, p.220.
160 * This method overrides the <code>markSupported</code> method in
161 * <code>InputStream</code> in order to return <code>true</code> -
162 * indicating that this stream class supports mark/reset
165 * @return <code>true</code> to indicate that this class supports
168 public boolean markSupported()
174 * This method reads one byte from the stream. The <code>pos</code>
175 * counter is advanced to the next byte to be read. The byte read is
176 * returned as an int in the range of 0-255. If the stream position
177 * is already at the end of the buffer, no byte is read and a -1 is
178 * returned in order to indicate the end of the stream.
180 * @return The byte read, or -1 if end of stream
182 public synchronized int read()
185 return ((int) buf[pos++]) & 0xFF;
190 * This method reads bytes from the stream and stores them into a
191 * caller supplied buffer. It starts storing the data at index
192 * <code>offset</code> into the buffer and attempts to read
193 * <code>len</code> bytes. This method can return before reading
194 * the number of bytes requested if the end of the stream is
195 * encountered first. The actual number of bytes read is returned.
196 * If no bytes can be read because the stream is already at the end
197 * of stream position, a -1 is returned.
199 * This method does not block.
201 * @param buffer The array into which the bytes read should be stored.
202 * @param offset The offset into the array to start storing bytes
203 * @param length The requested number of bytes to read
205 * @return The actual number of bytes read, or -1 if end of stream.
207 public synchronized int read(byte[] buffer, int offset, int length)
212 int numBytes = Math.min(count - pos, length);
213 System.arraycopy(buf, pos, buffer, offset, numBytes);
219 * This method sets the read position in the stream to the mark
220 * point by setting the <code>pos</code> variable equal to the
221 * <code>mark</code> variable. Since a mark can be set anywhere in
222 * the array, the mark/reset methods int this class can be used to
223 * provide random search capabilities for this type of stream.
225 public synchronized void reset()
231 * This method attempts to skip the requested number of bytes in the
232 * input stream. It does this by advancing the <code>pos</code>
233 * value by the specified number of bytes. It this would exceed the
234 * length of the buffer, then only enough bytes are skipped to
235 * position the stream at the end of the buffer. The actual number
236 * of bytes skipped is returned.
238 * @param num The requested number of bytes to skip
240 * @return The actual number of bytes skipped.
242 public synchronized long skip(long num)
244 // Even though the var numBytes is a long, in reality it can never
245 // be larger than an int since the result of subtracting 2 positive
246 // ints will always fit in an int. Since we have to return a long
247 // anyway, numBytes might as well just be a long.
248 long numBytes = Math.min((long) (count - pos), num < 0 ? 0L : num);