CHAPTER 22: The Package java.io Previous
Previous
Java Language
Java Language
Index
Index
Next
Next

22.6 The Class java.io.ByteArrayInputStream

22.6.1 buf , 22.6.2 pos , 22.6.3 count , 22.6.4 ByteArrayInputStream , 22.6.5 ByteArrayInputStream , 22.6.6 read , 22.6.7 read , 22.6.8 skip , 22.6.9 available , 22.6.10 reset

A ByteArrayInputStream contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the read method. See also StringBufferInputStream (S22.7).

public class ByteArrayInputStream extends InputStream {
	protected byte[] buf;
	protected int pos;
	protected int count;
	public ByteArrayInputStream(byte[] buf);
	public ByteArrayInputStream(byte[] buf,
			int offset, int length);
	public int read()
		throws NullPointerException, IndexOutOfBoundsException;
	public int read(byte[] b, int off, int len)
		throws NullPointerException, IndexOutOfBoundsException;
	public long skip(long n);
	public int available();
	public void reset();
}


22.6.1 buf

protected byte[] buf;

An array of bytes that was provided by the creator of the stream. Elements buf[0] through buf[count-1] are the only bytes that can ever be read from the stream; element buf[pos] is the next byte to be read.


22.6.2 pos

protected int pos;

This value should always be nonnegative and not larger than the value of count. The next byte to be read from this stream will be buf[pos].


22.6.3 count

protected int count;

This value should always be nonnegative and not larger than the length of buf. It is one greater than the position of the last byte within buf that can ever be read from this stream.


22.6.4 ByteArrayInputStream

public ByteArrayInputStream(byte[] buf)

This constructor initializes a newly created ByteArrayInputStream so that it uses buf as its buffer array. The initial value of pos is 0 and the initial value of count is the length of buf.


22.6.5 ByteArrayInputStream

public ByteArrayInputStream(byte[] buf,
	int offset, int length)

This constructor initializes a newly created ByteArrayInputStream so that it uses buf as its buffer array. The initial value of pos is offset and the initial value of count is offset+len.

Note that if bytes are simply read from the resulting input stream, elements buf[pos] through buf[pos+len-1] will be read; however, if a reset operation (S22.6.10) is performed, then bytes buf[0] through buf[pos-1] will then become available for input.


22.6.6 read

public int read()
	throws NullPointerException,       IndexOutOfBoundsException

If pos equals count, then -1 is returned to indicate end of file. Otherwise, the value buf[pos]&0xff is returned; just before the return, pos is incremented by 1.

Implements the read method of InputStream (S22.3.1).


22.6.7 read

public int read(byte[] b, int off, int len)
	throws NullPointerException,       IndexOutOfBoundsException

If pos equals count, then -1 is returned to indicate end of file. Otherwise, the number k of bytes read is equal to the smaller of len and count-pos. If k is positive, then bytes buf[pos] through buf[pos+k-1] are copied into b[off] through b[off+k-1] in the manner performed by System.arraycopy (S20.18.16). The value k is added into pos and k is returned.

Overrides the read method of InputStream (S22.3.3).


22.6.8 skip

public long skip(long n)

The actual number k of bytes to be skipped is equal to the smaller of n and count-pos. The value k is added into pos and k is returned.

Overrides the skip method of InputStream (S22.3.4).


22.6.9 available

public int available()

The quantity count-pos is returned.

Overrides the available method of InputStream (S22.3.5).


22.6.10 reset

public void reset()

The value of pos is set to 0.

Overrides the reset method of InputStream (S22.3.8).

Top© 1996 Sun Microsystems, Inc. All rights reserved.