论IO流之ByteArrayInputStream

      ByteArrayInputStream继承自InputStream抽象类,是以内存中的一个字节数组作为流来进行读写操作。在该流内中包含一个内部缓冲区数组,该缓冲区包含从流中读取的字节。源码如下:

public class ByteArrayInputStream extends InputStream {
    /**
     * 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.
     */
    protected byte buf[];

 该类包含两个构造方法:ByteArrayInputStream(byte buf[])和ByteArrayInputStream(byte buf[],int offset,int length),第一个是创建一个ByteArrayInputStream,使用buf作为其缓冲数组。第二个跟第一个构造方法差不多,只是偏移量的值是offset。

/**
     * Creates a ByteArrayInputStream
     * so that it  uses bufas its
     * buffer array. 
     * The buffer array is not copied. 
     * The initial value of pos
     * is 0 and the initial value
     * of  count is the length of
     * buf.
     *
     * @param   buf   the input buffer.
     */
public ByteArrayInputStream(byte buf[]) {
	this.buf = buf;
              this.pos = 0;
	this.count = buf.length;
}

/**
     * Creates ByteArrayInputStream
     * that uses buf as its
     * buffer array. The initial value of pos
     * is offset and the initial value
     * of count is the minimum of offset+length
     * and buf.length.
     * The buffer array is not copied. The buffer's mark is
     * set to the specified offset.
     *
     * @param   buf      the input buffer.
     * @param   offset   the offset in the buffer of the first byte to read.
     * @param   length   the maximum number of bytes to read from the buffer.
     */
public ByteArrayInputStream(byte buf[], int offset, int length) {
	this.buf = buf;
              this.pos = offset;
	this.count = Math.min(offset + length, buf.length);
              this.mark = offset;
}
 该类的read()方法是从输入流中读取下一个数据字节。返回一个0到255范围内的int字节值。如果到达流末尾则返回-1。
/**
     * Reads the next byte of data from this input stream. The value 
     * byte is returned as an int in the range 
     * 0 to 255. If no byte is available 
     * because the end of the stream has been reached, the value 
     * -1 is returned. 
     * This read method 
     * cannot block. 
     *
     * @return  the next byte of data, or -1 if the end of the
     *          stream has been reached.
     */
public synchronized int read() {
	return (pos < count) ? (buf[pos++] & 0xff) : -1;
}
 在该类里面,还有个方法要注意下就是skip(long n)方法。该方法指从输入流中跳过n个输入字节,从方法中可以看出如果已经到达流的末尾,则实际跳过的字节值是n和count-pos中的较小值。
/**
     * Skips n bytes of input from this input stream. Fewer 
     * bytes might be skipped if the end of the input stream is reached. 
     * 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.
     *
     * @param   n   the number of bytes to be skipped.
     * @return  the actual number of bytes skipped.
     */
public synchronized long skip(long n) {
	if (pos + n > count) {
	    n = count - pos;
	}
	if (n < 0) {
	    return 0;
	}
	pos += n;
	return n;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值