ByteArrayInputStream源码解析

测试代码
package bt;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

public class OutPutStreamTest {

    public static void main(String[] args) throws IOException {
    //定义一个字符串,长度为11
        String msg = "hello world";
        ByteArrayInputStream input = new ByteArrayInputStream(msg.getBytes());
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] data = new byte[10];
        int tmp = 0;
        //定义一个data去接收内存中的值,初始化长度为10
        while((tmp = input.read(data)) > 0){
            output.write(data);
        }
        System.out.println(output);
    }
}
结果为:
hello worldello worl
我们发现结果不符合预期,这是因为每次读取数据之前,不清空data上次的残留数据,源码分析有说明
怎么解决这个问题?
需要我们自己手动指定每次读取的位置:output.write(data,0,tmp);
当然你要是不嫌麻烦,也可以手动清空,清空方法:data[inedx]=0
package java.io;

public
class ByteArrayInputStream extends InputStream {

    protected byte buf[];

    protected int pos;

    protected int mark = 0;

    protected int count;

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

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

    //我们发现这个方法每次读之前并没有清空字节数组,可能会导致上一次读取的内容残留,这一点,我们需要自己解决
    //off 在数组b在其中写入数据的起始位置的偏移
    //len  要读取的字节数
    //比如byte[] data = new byte[10];
    //read(data,1,3)就表示数据读到数组中的第二个位置,每次只读三个存进出,所以这个时候取值就要注意了,取值的起始位置应该是1,否则预期结果将不一致,即output.write(data,1,tmp);
    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;
        }
        //字节数组中剩余数量
        int avail = count - pos;
        //如果自定义数组的长度大于剩余字节数组中未读的数组数量
        if (len > avail) {
            len = avail;
        }
        if (len <= 0) {
            return 0;
        }
        //改方法调用的是系统函数,有兴趣的可以自己了解一下windows版JVM+windows本地函数
        System.arraycopy(buf, pos, b, off, len);
        //pos:postion 位置 这里姑且理解为指针,每一次读完之后,内存中的存放数据的数组位置向右滑动len个单位,读取下一帧
        pos += len;
        return len;
    }

    public synchronized long skip(long n) {
        long k = count - pos;
        if (n < k) {
            k = n < 0 ? 0 : n;
        }

        pos += k;
        return k;
    }

    public synchronized int available() {
        return count - pos;
    }

    public boolean markSupported() {
        return true;
    }
    //这个方法是和下面的reset方法结合着用的,这个是把重要的位置打标记,下面的是从标记的位置开始取值,即:重置,默认为标记点为0
    public void mark(int readAheadLimit) {
        mark = pos;
    }
    public synchronized void reset() {
        pos = mark;
    }

    public void close() throws IOException {
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值