001    /* ByteArrayInputStream.java -- Read an array as a stream
002       Copyright (C) 1998, 1999, 2001, 2005  Free Software Foundation, Inc.
003    
004    This file is part of GNU Classpath.
005    
006    GNU Classpath is free software; you can redistribute it and/or modify
007    it under the terms of the GNU General Public License as published by
008    the Free Software Foundation; either version 2, or (at your option)
009    any later version.
010     
011    GNU Classpath is distributed in the hope that it will be useful, but
012    WITHOUT ANY WARRANTY; without even the implied warranty of
013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014    General Public License for more details.
015    
016    You should have received a copy of the GNU General Public License
017    along with GNU Classpath; see the file COPYING.  If not, write to the
018    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019    02110-1301 USA.
020    
021    Linking this library statically or dynamically with other modules is
022    making a combined work based on this library.  Thus, the terms and
023    conditions of the GNU General Public License cover the whole
024    combination.
025    
026    As a special exception, the copyright holders of this library give you
027    permission to link this library with independent modules to produce an
028    executable, regardless of the license terms of these independent
029    modules, and to copy and distribute the resulting executable under
030    terms of your choice, provided that you also meet, for each linked
031    independent module, the terms and conditions of the license of that
032    module.  An independent module is a module which is not derived from
033    or based on this library.  If you modify this library, you may extend
034    this exception to your version of the library, but you are not
035    obligated to do so.  If you do not wish to do so, delete this
036    exception statement from your version. */
037    
038    
039    package java.io;
040    
041    /**
042      * This class permits an array of bytes to be read as an input stream.
043      *
044      * @author Warren Levy (warrenl@cygnus.com)
045      * @author Aaron M. Renn (arenn@urbanophile.com)
046      */ 
047    public class ByteArrayInputStream extends InputStream
048    {
049      /**
050       * The array that contains the data supplied during read operations
051       */
052      protected byte[] buf;
053    
054      /**
055       * The array index of the next byte to be read from the buffer
056       * <code>buf</code>
057       */
058      protected int pos;
059    
060      /**
061       * The currently marked position in the stream.  This defaults to 0, so a
062       * reset operation on the stream resets it to read from array index 0 in
063       * the buffer - even if the stream was initially created with an offset
064       * greater than 0
065       */
066      protected int mark;
067    
068      /**
069       * This indicates the maximum number of bytes that can be read from this
070       * stream.  It is the array index of the position after the last valid
071       * byte in the buffer <code>buf</code>
072       */
073      protected int count;
074    
075      /**
076       * Create a new ByteArrayInputStream that will read bytes from the passed
077       * in byte array.  This stream will read from the beginning to the end
078       * of the array.  It is identical to calling an overloaded constructor
079       * as <code>ByteArrayInputStream(buf, 0, buf.length)</code>.
080       * <p>
081       * Note that this array is not copied.  If its contents are changed 
082       * while this stream is being read, those changes will be reflected in the
083       * bytes supplied to the reader.  Please use caution in changing the 
084       * contents of the buffer while this stream is open.
085       *
086       * @param buffer The byte array buffer this stream will read from.
087       */
088      public ByteArrayInputStream(byte[] buffer)
089      {
090        this(buffer, 0, buffer.length);
091      }
092    
093      /**
094       * Create a new ByteArrayInputStream that will read bytes from the
095       * passed in byte array.  This stream will read from position
096       * <code>offset</code> in the array for a length of
097       * <code>length</code> bytes past <code>offset</code>.  If the
098       * stream is reset to a position before <code>offset</code> then
099       * more than <code>length</code> bytes can be read from the stream.
100       * The <code>length</code> value should be viewed as the array index
101       * one greater than the last position in the buffer to read.
102       * <p>
103       * Note that this array is not copied.  If its contents are changed 
104       * while this stream is being read, those changes will be reflected in the
105       * bytes supplied to the reader.  Please use caution in changing the 
106       * contents of the buffer while this stream is open.
107       *
108       * @param buffer The byte array buffer this stream will read from.
109       * @param offset The index into the buffer to start reading bytes from
110       * @param length The number of bytes to read from the buffer
111       */
112      public ByteArrayInputStream(byte[] buffer, int offset, int length)
113      {
114        if (offset < 0  || length < 0 || offset > buffer.length)
115          throw new IllegalArgumentException();
116    
117        buf = buffer;
118    
119        count = offset + length;
120        if (count > buf.length)
121          count = buf.length;
122    
123        pos = offset;
124        mark = pos;
125      }
126    
127      /**
128       * This method returns the number of bytes available to be read from this
129       * stream.  The value returned will be equal to <code>count - pos</code>.
130       *
131       * @return The number of bytes that can be read from this stream
132       * before blocking, which is all of them 
133       */
134      public synchronized int available()
135      {
136        return count - pos;
137      }
138    
139      /**
140       * This method sets the mark position in this stream to the current
141       * position.  Note that the <code>readlimit</code> parameter in this
142       * method does nothing as this stream is always capable of
143       * remembering all the bytes int it.
144       * <p>
145       * Note that in this class the mark position is set by default to
146       * position 0 in the stream.  This is in constrast to some other
147       * stream types where there is no default mark position.
148       *
149       * @param readLimit The number of bytes this stream must remember.
150       * This parameter is ignored.
151       */
152      public synchronized void mark(int readLimit)
153      {
154        // readLimit is ignored per Java Class Lib. book, p.220.
155        mark = pos;
156      }
157    
158      /**
159       * This method overrides the <code>markSupported</code> method in
160       * <code>InputStream</code> in order to return <code>true</code> -
161       * indicating that this stream class supports mark/reset
162       * functionality.
163       *
164       * @return <code>true</code> to indicate that this class supports
165       * mark/reset.
166       */
167      public boolean markSupported()
168      {
169        return true;
170      }
171    
172      /**
173       * This method reads one byte from the stream.  The <code>pos</code>
174       * counter is advanced to the next byte to be read.  The byte read is
175       * returned as an int in the range of 0-255.  If the stream position
176       * is already at the end of the buffer, no byte is read and a -1 is
177       * returned in order to indicate the end of the stream.
178       *
179       * @return The byte read, or -1 if end of stream
180       */
181      public synchronized int read()
182      {
183        if (pos < count)
184          return ((int) buf[pos++]) & 0xFF;
185        return -1;
186      }
187    
188      /**
189       * This method reads bytes from the stream and stores them into a
190       * caller supplied buffer.  It starts storing the data at index
191       * <code>offset</code> into the buffer and attempts to read
192       * <code>len</code> bytes.  This method can return before reading
193       * the number of bytes requested if the end of the stream is
194       * encountered first.  The actual number of bytes read is returned.
195       * If no bytes can be read because the stream is already at the end
196       * of stream position, a -1 is returned.
197       * <p>
198       * This method does not block.
199       *
200       * @param buffer The array into which the bytes read should be stored.
201       * @param offset The offset into the array to start storing bytes
202       * @param length The requested number of bytes to read
203       *
204       * @return The actual number of bytes read, or -1 if end of stream.
205       */
206      public synchronized int read(byte[] buffer, int offset, int length)
207      {
208        if (pos >= count)
209          return -1;
210    
211        int numBytes = Math.min(count - pos, length);
212        System.arraycopy(buf, pos, buffer, offset, numBytes);
213        pos += numBytes;
214        return numBytes;
215      }
216    
217      /**
218       * This method sets the read position in the stream to the mark
219       * point by setting the <code>pos</code> variable equal to the
220       * <code>mark</code> variable.  Since a mark can be set anywhere in
221       * the array, the mark/reset methods int this class can be used to
222       * provide random search capabilities for this type of stream.
223       */
224      public synchronized void reset()
225      {
226        pos = mark;
227      }
228    
229      /**
230       * This method attempts to skip the requested number of bytes in the
231       * input stream.  It does this by advancing the <code>pos</code>
232       * value by the specified number of bytes.  It this would exceed the
233       * length of the buffer, then only enough bytes are skipped to
234       * position the stream at the end of the buffer.  The actual number
235       * of bytes skipped is returned.
236       *
237       * @param num The requested number of bytes to skip
238       *
239       * @return The actual number of bytes skipped.
240       */
241      public synchronized long skip(long num)
242      {
243        // Even though the var numBytes is a long, in reality it can never
244        // be larger than an int since the result of subtracting 2 positive
245        // ints will always fit in an int.  Since we have to return a long
246        // anyway, numBytes might as well just be a long.
247        long numBytes = Math.min((long) (count - pos), num < 0 ? 0L : num);
248        pos += numBytes;
249        return numBytes;
250      }
251    }