try to make mp3decoder pass SSJava checking
[IRC.git] / Robust / src / ClassLibrary / SSJava / ByteArrayInputStream.java
1 /* ByteArrayInputStream.java -- Read an array as a stream
2    Copyright (C) 1998, 1999, 2001, 2005  Free Software Foundation, Inc.
3
4 This file is part of GNU Classpath.
5
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)
9 any later version.
10  
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.
15
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
19 02110-1301 USA.
20
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
24 combination.
25
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. */
37
38
39 //package java.io;
40
41 /**
42   * This class permits an array of bytes to be read as an input stream.
43   *
44   * @author Warren Levy (warrenl@cygnus.com)
45   * @author Aaron M. Renn (arenn@urbanophile.com)
46   */ 
47 @LATTICE("")
48 public class ByteArrayInputStream extends InputStream
49 {
50   /**
51    * The array that contains the data supplied during read operations
52    */
53   protected byte[] buf;
54
55   /**
56    * The array index of the next byte to be read from the buffer
57    * <code>buf</code>
58    */
59   protected int pos;
60
61   /**
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
65    * greater than 0
66    */
67   protected int mark;
68
69   /**
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>
73    */
74   protected int count;
75
76   /**
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>.
81    * <p>
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.
86    *
87    * @param buffer The byte array buffer this stream will read from.
88    */
89   public ByteArrayInputStream(byte[] buffer)
90   {
91     this(buffer, 0, buffer.length);
92   }
93
94   /**
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.
103    * <p>
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.
108    *
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
112    */
113   public ByteArrayInputStream(byte[] buffer, int offset, int length)
114   {
115     if (offset < 0  || length < 0 || offset > buffer.length)
116       throw new IllegalArgumentException();
117
118     buf = buffer;
119
120     count = offset + length;
121     if (count > buf.length)
122       count = buf.length;
123
124     pos = offset;
125     mark = pos;
126   }
127
128   /**
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>.
131    *
132    * @return The number of bytes that can be read from this stream
133    * before blocking, which is all of them 
134    */
135   public synchronized int available()
136   {
137     return count - pos;
138   }
139
140   /**
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.
145    * <p>
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.
149    *
150    * @param readLimit The number of bytes this stream must remember.
151    * This parameter is ignored.
152    */
153   public synchronized void mark(int readLimit)
154   {
155     // readLimit is ignored per Java Class Lib. book, p.220.
156     mark = pos;
157   }
158
159   /**
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
163    * functionality.
164    *
165    * @return <code>true</code> to indicate that this class supports
166    * mark/reset.
167    */
168   public boolean markSupported()
169   {
170     return true;
171   }
172
173   /**
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.
179    *
180    * @return The byte read, or -1 if end of stream
181    */
182   public synchronized int read()
183   {
184     if (pos < count)
185       return ((int) buf[pos++]) & 0xFF;
186     return -1;
187   }
188
189   /**
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.
198    * <p>
199    * This method does not block.
200    *
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
204    *
205    * @return The actual number of bytes read, or -1 if end of stream.
206    */
207   public synchronized int read(byte[] buffer, int offset, int length)
208   {
209     if (pos >= count)
210       return -1;
211
212     int numBytes = Math.min(count - pos, length);
213     System.arraycopy(buf, pos, buffer, offset, numBytes);
214     pos += numBytes;
215     return numBytes;
216   }
217
218   /**
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.
224    */
225   public synchronized void reset()
226   {
227     pos = mark;
228   }
229
230   /**
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.
237    *
238    * @param num The requested number of bytes to skip
239    *
240    * @return The actual number of bytes skipped.
241    */
242   public synchronized long skip(long num)
243   {
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);
249     pos +=(int)numBytes;
250     return numBytes;
251   }
252 }