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
23 * questions.
24 */
25
26 package java.io;
27
28 /**
29 * A <code>ByteArrayInputStream</code> contains
30 * an internal buffer that contains bytes that
31 * may be read from the stream. An internal
32 * counter keeps track of the next byte to
33 * be supplied by the <code>read</code> method.
34 * <p>
35 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
36 * this class can be called after the stream has been closed without
37 * generating an <tt>IOException</tt>.
38 *
39 * @author Arthur van Hoff
40 * @see java.io.StringBufferInputStream
41 * @since JDK1.0
42 */
43 public
44 class ByteArrayInputStream extends InputStream {
45
46 /**
47 * An array of bytes that was provided
48 * by the creator of the stream. Elements <code>buf[0]</code>
49 * through <code>buf[count-1]</code> are the
50 * only bytes that can ever be read from the
51 * stream; element <code>buf[pos]</code> is
52 * the next byte to be read.
53 */
54 protected byte buf[];
55
56 /**
57 * The index of the next character to read from the input stream buffer.
58 * This value should always be nonnegative
59 * and not larger than the value of <code>count</code>.
60 * The next byte to be read from the input stream buffer
61 * will be <code>buf[pos]</code>.
62 */
63 protected int pos;
64
65 /**
66 * The currently marked position in the stream.
67 * ByteArrayInputStream objects are marked at position zero by
68 * default when constructed. They may be marked at another
69 * position within the buffer by the <code>mark()</code> method.
70 * The current buffer position is set to this point by the
71 * <code>reset()</code> method.
72 * <p>
73 * If no mark has been set, then the value of mark is the offset
74 * passed to the constructor (or 0 if the offset was not supplied).
75 *
76 * @since JDK1.1
77 */
78 protected int mark = 0;
79
80 /**
81 * The index one greater than the last valid character in the input
82 * stream buffer.
83 * This value should always be nonnegative
84 * and not larger than the length of <code>buf</code>.
85 * It is one greater than the position of
86 * the last byte within <code>buf</code> that
87 * can ever be read from the input stream buffer.
88 */
89 protected int count;
90
91 /**
92 * Creates a <code>ByteArrayInputStream</code>
93 * so that it uses <code>buf</code> as its
94 * buffer array.
95 * The buffer array is not copied.
96 * The initial value of <code>pos</code>
220
221 /**
222 * Returns the number of remaining bytes that can be read (or skipped over)
223 * from this input stream.
224 * <p>
225 * The value returned is <code>count - pos</code>,
226 * which is the number of bytes remaining to be read from the input buffer.
227 *
228 * @return the number of remaining bytes that can be read (or skipped
229 * over) from this input stream without blocking.
230 */
231 public synchronized int available() {
232 return count - pos;
233 }
234
235 /**
236 * Tests if this <code>InputStream</code> supports mark/reset. The
237 * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
238 * always returns <code>true</code>.
239 *
240 * @since JDK1.1
241 */
242 public boolean markSupported() {
243 return true;
244 }
245
246 /**
247 * Set the current marked position in the stream.
248 * ByteArrayInputStream objects are marked at position zero by
249 * default when constructed. They may be marked at another
250 * position within the buffer by this method.
251 * <p>
252 * If no mark has been set, then the value of the mark is the
253 * offset passed to the constructor (or 0 if the offset was not
254 * supplied).
255 *
256 * <p> Note: The <code>readAheadLimit</code> for this class
257 * has no meaning.
258 *
259 * @since JDK1.1
260 */
261 public void mark(int readAheadLimit) {
262 mark = pos;
263 }
264
265 /**
266 * Resets the buffer to the marked position. The marked position
267 * is 0 unless another position was marked or an offset was specified
268 * in the constructor.
269 */
270 public synchronized void reset() {
271 pos = mark;
272 }
273
274 /**
275 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
276 * this class can be called after the stream has been closed without
277 * generating an <tt>IOException</tt>.
278 */
279 public void close() throws IOException {
|
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
23 * questions.
24 */
25
26 package java.io;
27
28 /**
29 * A <code>ByteArrayInputStream</code> contains
30 * an internal buffer that contains bytes that
31 * may be read from the stream. An internal
32 * counter keeps track of the next byte to
33 * be supplied by the <code>read</code> method.
34 * <p>
35 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
36 * this class can be called after the stream has been closed without
37 * generating an <tt>IOException</tt>.
38 *
39 * @author Arthur van Hoff
40 * @see java.io.StringBufferInputStream
41 * @since 1.0
42 */
43 public
44 class ByteArrayInputStream extends InputStream {
45
46 /**
47 * An array of bytes that was provided
48 * by the creator of the stream. Elements <code>buf[0]</code>
49 * through <code>buf[count-1]</code> are the
50 * only bytes that can ever be read from the
51 * stream; element <code>buf[pos]</code> is
52 * the next byte to be read.
53 */
54 protected byte buf[];
55
56 /**
57 * The index of the next character to read from the input stream buffer.
58 * This value should always be nonnegative
59 * and not larger than the value of <code>count</code>.
60 * The next byte to be read from the input stream buffer
61 * will be <code>buf[pos]</code>.
62 */
63 protected int pos;
64
65 /**
66 * The currently marked position in the stream.
67 * ByteArrayInputStream objects are marked at position zero by
68 * default when constructed. They may be marked at another
69 * position within the buffer by the <code>mark()</code> method.
70 * The current buffer position is set to this point by the
71 * <code>reset()</code> method.
72 * <p>
73 * If no mark has been set, then the value of mark is the offset
74 * passed to the constructor (or 0 if the offset was not supplied).
75 *
76 * @since 1.1
77 */
78 protected int mark = 0;
79
80 /**
81 * The index one greater than the last valid character in the input
82 * stream buffer.
83 * This value should always be nonnegative
84 * and not larger than the length of <code>buf</code>.
85 * It is one greater than the position of
86 * the last byte within <code>buf</code> that
87 * can ever be read from the input stream buffer.
88 */
89 protected int count;
90
91 /**
92 * Creates a <code>ByteArrayInputStream</code>
93 * so that it uses <code>buf</code> as its
94 * buffer array.
95 * The buffer array is not copied.
96 * The initial value of <code>pos</code>
220
221 /**
222 * Returns the number of remaining bytes that can be read (or skipped over)
223 * from this input stream.
224 * <p>
225 * The value returned is <code>count - pos</code>,
226 * which is the number of bytes remaining to be read from the input buffer.
227 *
228 * @return the number of remaining bytes that can be read (or skipped
229 * over) from this input stream without blocking.
230 */
231 public synchronized int available() {
232 return count - pos;
233 }
234
235 /**
236 * Tests if this <code>InputStream</code> supports mark/reset. The
237 * <code>markSupported</code> method of <code>ByteArrayInputStream</code>
238 * always returns <code>true</code>.
239 *
240 * @since 1.1
241 */
242 public boolean markSupported() {
243 return true;
244 }
245
246 /**
247 * Set the current marked position in the stream.
248 * ByteArrayInputStream objects are marked at position zero by
249 * default when constructed. They may be marked at another
250 * position within the buffer by this method.
251 * <p>
252 * If no mark has been set, then the value of the mark is the
253 * offset passed to the constructor (or 0 if the offset was not
254 * supplied).
255 *
256 * <p> Note: The <code>readAheadLimit</code> for this class
257 * has no meaning.
258 *
259 * @since 1.1
260 */
261 public void mark(int readAheadLimit) {
262 mark = pos;
263 }
264
265 /**
266 * Resets the buffer to the marked position. The marked position
267 * is 0 unless another position was marked or an offset was specified
268 * in the constructor.
269 */
270 public synchronized void reset() {
271 pos = mark;
272 }
273
274 /**
275 * Closing a <tt>ByteArrayInputStream</tt> has no effect. The methods in
276 * this class can be called after the stream has been closed without
277 * generating an <tt>IOException</tt>.
278 */
279 public void close() throws IOException {
|