数组流源码解读和基本用法

继承关系:

class ByteArrayInputStream extends InputStream
ByteArrayInputStream继承自InputStream

class ByteArrayOutputStream extends OutputStream
ByteArrayOutputStream继承自OutputStream

属性:

ByteArrayInputStream:

protected byte buf[];//数据源

protected int pos;//当前指针位置

protected int mark = 0;//当前标记位置,默认0

protected int count;//数据源中元素的有效个数

ByteArrayOutputStream:

protected byte buf[];//将要写入的数组

protected int count;//数组中元素的有效个数

构造函数:

ByteArrayInputStream:

public ByteArrayInputStream(byte buf[]) {
    this.buf = buf;
    this.pos = 0;
    this.count = buf.length;
}

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;
}

ByteArrayOutputStream:

public ByteArrayOutputStream() {//数组默认大小32

    this(32);
}

public ByteArrayOutputStream(int size) {
    if (size < 0) {
        throw new IllegalArgumentException("Negative initial size: "
                                           + size);
    }
    buf = new byte[size];
}

基本方法:

读:

public synchronized int read() {
return (pos < count) ? (buf[pos++] & 0xff) : -1;
}

/**
*byte b[]:要将读取数据存放的数组b[]
*int off:将要在b[]中存储位置的偏移量
*int len:要读取的长度
*/
public synchronized int read(byte b[], int off, int len) {
if (b == null) {
throw new NullPointerException();
} else if (off < 0 || len < 0 || len > b.length - off) {
throw new IndexOutOfBoundsException();
}
if (pos >= count) {
return -1;
}
if (pos + len > count) {
len = count - pos;
}
if (len <= 0) {
return 0;
}
System.arraycopy(buf, pos, b, off, len);
pos += len; //读取len个元素后移动指针
return len; //返回读取的长度
}

//指针向后移n个位置
public synchronized long skip(long n) {
if (pos + n > count) {
n = count - pos;
}
if (n < 0) {
return 0;
}
pos += n;
return n;
}

//得到目前还能读取的长度
public synchronized int available() {
return count - pos;
}

//默认可标记
public boolean markSupported() {
return true;
}

//标记当前指针所指位置
public void mark(int readAheadLimit) {//该参数无意义
mark = pos;
}

//指针回到标记位置
public synchronized void reset() {
pos = mark;
}

写:

//将b写入buf数组中
public synchronized void write(int b) {
int newcount = count + 1;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
//扩容方式:数组长度的二倍和newcount中的最大值
}
buf[count] = (byte)b;
count = newcount;
}

/**
*byte b[]:将要写入的数据源
*int off:将要写入的指针在b[]中的偏移量
*int len:要写入的长度
*/
public synchronized void write(byte b[], int off, int len) {
if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
int newcount = count + len;
if (newcount > buf.length) {
buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount));
//扩容
}
System.arraycopy(b, off, buf, count, len);//数组拷贝
count = newcount;
}

//传入参数为输出流
public synchronized void writeTo(OutputStream out) throws IOException {
out.write(buf, 0, count);
}

//清除buf数组中的元素
public synchronized void reset() {
count = 0;
}

test:

import java.io.ByteArrayInputStream;
import java.util.Arrays;

public class ByteArray {
    public static void main(String[] args) {
        byte b[]= new byte[]{1, 2, 3, 4, 5, 6};
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(b
);
        byte c[]=new byte[10];
        byteArrayInputStream.read(c,1,2);
        System.out.println(Arrays.toString(c)); //0,1,2
        int a=byteArrayInputStream.available();//4
        System.out.println(a);
        if (byteArrayInputStream.markSupported()) {
            byteArrayInputStream.mark(0);
        }
        byteArrayInputStream.skip(2);
        System.out.println(byteArrayInputStream.read());//5
        byteArrayInputStream.reset();
        System.out.println(byteArrayInputStream.read());//3
    }
}

运行结果:
[0, 1, 2, 0, 0, 0, 0, 0, 0, 0]
4
5
3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值