KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > io > ByteArrayInputStream


1 /*
2  * @(#)ByteArrayInputStream.java 1.44 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.io;
9
10 /**
11  * A <code>ByteArrayInputStream</code> contains
12  * an internal buffer that contains bytes that
13  * may be read from the stream. An internal
14  * counter keeps track of the next byte to
15  * be supplied by the <code>read</code> method.
16  * <p>
17  * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
18  * this class can be called after the stream has been closed without
19  * generating an <tt>IOException</tt>.
20  *
21  * @author Arthur van Hoff
22  * @version 1.44, 12/19/03
23  * @see java.io.StringBufferInputStream
24  * @since JDK1.0
25  */

26 public
27 class ByteArrayInputStream extends InputStream JavaDoc {
28
29     /**
30      * An array of bytes that was provided
31      * by the creator of the stream. Elements <code>buf[0]</code>
32      * through <code>buf[count-1]</code> are the
33      * only bytes that can ever be read from the
34      * stream; element <code>buf[pos]</code> is
35      * the next byte to be read.
36      */

37     protected byte buf[];
38
39     /**
40      * The index of the next character to read from the input stream buffer.
41      * This value should always be nonnegative
42      * and not larger than the value of <code>count</code>.
43      * The next byte to be read from the input stream buffer
44      * will be <code>buf[pos]</code>.
45      */

46     protected int pos;
47
48     /**
49      * The currently marked position in the stream.
50      * ByteArrayInputStream objects are marked at position zero by
51      * default when constructed. They may be marked at another
52      * position within the buffer by the <code>mark()</code> method.
53      * The current buffer position is set to this point by the
54      * <code>reset()</code> method.
55      * <p>
56      * If no mark has been set, then the value of mark is the offset
57      * passed to the constructor (or 0 if the offset was not supplied).
58      *
59      * @since JDK1.1
60      */

61     protected int mark = 0;
62
63     /**
64      * The index one greater than the last valid character in the input
65      * stream buffer.
66      * This value should always be nonnegative
67      * and not larger than the length of <code>buf</code>.
68      * It is one greater than the position of
69      * the last byte within <code>buf</code> that
70      * can ever be read from the input stream buffer.
71      */

72     protected int count;
73
74     /**
75      * Creates a <code>ByteArrayInputStream</code>
76      * so that it uses <code>buf</code> as its
77      * buffer array.
78      * The buffer array is not copied.
79      * The initial value of <code>pos</code>
80      * is <code>0</code> and the initial value
81      * of <code>count</code> is the length of
82      * <code>buf</code>.
83      *
84      * @param buf the input buffer.
85      */

86     public ByteArrayInputStream(byte buf[]) {
87     this.buf = buf;
88         this.pos = 0;
89     this.count = buf.length;
90     }
91
92     /**
93      * Creates <code>ByteArrayInputStream</code>
94      * that uses <code>buf</code> as its
95      * buffer array. The initial value of <code>pos</code>
96      * is <code>offset</code> and the initial value
97      * of <code>count</code> is the minimum of <code>offset+length</code>
98      * and <code>buf.length</code>.
99      * The buffer array is not copied. The buffer's mark is
100      * set to the specified offset.
101      *
102      * @param buf the input buffer.
103      * @param offset the offset in the buffer of the first byte to read.
104      * @param length the maximum number of bytes to read from the buffer.
105      */

106     public ByteArrayInputStream(byte buf[], int offset, int length) {
107     this.buf = buf;
108         this.pos = offset;
109     this.count = Math.min(offset + length, buf.length);
110         this.mark = offset;
111     }
112
113     /**
114      * Reads the next byte of data from this input stream. The value
115      * byte is returned as an <code>int</code> in the range
116      * <code>0</code> to <code>255</code>. If no byte is available
117      * because the end of the stream has been reached, the value
118      * <code>-1</code> is returned.
119      * <p>
120      * This <code>read</code> method
121      * cannot block.
122      *
123      * @return the next byte of data, or <code>-1</code> if the end of the
124      * stream has been reached.
125      */

126     public synchronized int read() {
127     return (pos < count) ? (buf[pos++] & 0xff) : -1;
128     }
129
130     /**
131      * Reads up to <code>len</code> bytes of data into an array of bytes
132      * from this input stream.
133      * If <code>pos</code> equals <code>count</code>,
134      * then <code>-1</code> is returned to indicate
135      * end of file. Otherwise, the number <code>k</code>
136      * of bytes read is equal to the smaller of
137      * <code>len</code> and <code>count-pos</code>.
138      * If <code>k</code> is positive, then bytes
139      * <code>buf[pos]</code> through <code>buf[pos+k-1]</code>
140      * are copied into <code>b[off]</code> through
141      * <code>b[off+k-1]</code> in the manner performed
142      * by <code>System.arraycopy</code>. The
143      * value <code>k</code> is added into <code>pos</code>
144      * and <code>k</code> is returned.
145      * <p>
146      * This <code>read</code> method cannot block.
147      *
148      * @param b the buffer into which the data is read.
149      * @param off the start offset of the data.
150      * @param len the maximum number of bytes read.
151      * @return the total number of bytes read into the buffer, or
152      * <code>-1</code> if there is no more data because the end of
153      * the stream has been reached.
154      */

155     public synchronized int read(byte b[], int off, int len) {
156     if (b == null) {
157         throw new NullPointerException JavaDoc();
158     } else if ((off < 0) || (off > b.length) || (len < 0) ||
159            ((off + len) > b.length) || ((off + len) < 0)) {
160         throw new IndexOutOfBoundsException JavaDoc();
161     }
162     if (pos >= count) {
163         return -1;
164     }
165     if (pos + len > count) {
166         len = count - pos;
167     }
168     if (len <= 0) {
169         return 0;
170     }
171     System.arraycopy(buf, pos, b, off, len);
172     pos += len;
173     return len;
174     }
175
176     /**
177      * Skips <code>n</code> bytes of input from this input stream. Fewer
178      * bytes might be skipped if the end of the input stream is reached.
179      * The actual number <code>k</code>
180      * of bytes to be skipped is equal to the smaller
181      * of <code>n</code> and <code>count-pos</code>.
182      * The value <code>k</code> is added into <code>pos</code>
183      * and <code>k</code> is returned.
184      *
185      * @param n the number of bytes to be skipped.
186      * @return the actual number of bytes skipped.
187      */

188     public synchronized long skip(long n) {
189     if (pos + n > count) {
190         n = count - pos;
191     }
192     if (n < 0) {
193         return 0;
194     }
195     pos += n;
196     return n;
197     }
198
199     /**
200      * Returns the number of bytes that can be read from this input
201      * stream without blocking.
202      * The value returned is
203      * <code>count&nbsp;- pos</code>,
204      * which is the number of bytes remaining to be read from the input buffer.
205      *
206      * @return the number of bytes that can be read from the input stream
207      * without blocking.
208      */

209     public synchronized int available() {
210     return count - pos;
211     }
212
213     /**
214      * Tests if this <code>InputStream</code> supports mark/reset. The
215      * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
216      * always returns <code>true</code>.
217      *
218      * @since JDK1.1
219      */

220     public boolean markSupported() {
221     return true;
222     }
223
224     /**
225      * Set the current marked position in the stream.
226      * ByteArrayInputStream objects are marked at position zero by
227      * default when constructed. They may be marked at another
228      * position within the buffer by this method.
229      * <p>
230      * If no mark has been set, then the value of the mark is the
231      * offset passed to the constructor (or 0 if the offset was not
232      * supplied).
233      *
234      * <p> Note: The <code>readAheadLimit</code> for this class
235      * has no meaning.
236      *
237      * @since JDK1.1
238      */

239     public void mark(int readAheadLimit) {
240     mark = pos;
241     }
242
243     /**
244      * Resets the buffer to the marked position. The marked position
245      * is 0 unless another position was marked or an offset was specified
246      * in the constructor.
247      */

248     public synchronized void reset() {
249     pos = mark;
250     }
251
252     /**
253      * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
254      * this class can be called after the stream has been closed without
255      * generating an <tt>IOException</tt>.
256      * <p>
257      */

258     public void close() throws IOException JavaDoc {
259     }
260
261 }
262
Popular Tags