BufferedOutputStream

第一步:先贴源码

public class BufferedOutputStream extends FilterOutputStream {

    protected byte buf[];

    protected int count;

    // 创建一个BufferedOutputStream,默认buf缓存的大小为8192
    public BufferedOutputStream(OutputStream out) {
        this(out, 8192);
    }

    // 创建一个指定大小的buf缓存 
    public BufferedOutputStream(OutputStream out, int size) {
        super(out);
        if (size <= 0) {
            throw new IllegalArgumentException("Buffer size <= 0");
        }
        buf = new byte[size];
    }

    /** Flush the internal buffer */
    private void flushBuffer() throws IOException {
        if (count > 0) {
            out.write(buf, 0, count);
            count = 0;
        }
    }


    // 判断buf[]缓存是否存满,存满则flush,不满,则继续将byte存入到buf缓存中
    public synchronized void write(int b) throws IOException {
        if (count >= buf.length) {
            flushBuffer();
        }
        buf[count++] = (byte)b;
    }

    // 写入指定长度的byte数组 
    public synchronized void write(byte b[], int off, int len) throws IOException {
        // 如果写入数组的长度大于buf[]缓存的长度
        if (len >= buf.length) {
            /* If the request length exceeds the size of the output buffer,
               flush the output buffer and then write the data directly.
               In this way buffered streams will cascade harmlessly. */
            // 将buf缓存中数据flush
            flushBuffer();
            // 直接写数据,不需要存入到缓存中
            out.write(b, off, len);
            return;
        }
        // 如果写入数组的长度和buf缓存中已经写入数据的长度之和大于buf缓存的总长度,第一步先将缓存中的数据flush(),因为写入的长度肯定小于buf缓存的总长度(上一步判断),并且通过flush(),buf缓存清空,所以之后将需要写入的数组就可以直接复制到buf缓存中。
        if (len > buf.length - count) {
            flushBuffer();
        }
        // 调用System.arraycopy()的二种情况:第一种:flushBuffer()之后,存放写入数组。第二种:写入数组长度加上buf缓存的count之和也小于buf缓存的长度。
        System.arraycopy(b, off, buf, count, len);
        count += len;
    }

    public synchronized void flush() throws IOException {
        flushBuffer();
        out.flush();
    }
}

第二点:BufferedOutputStream注意点

第一:BufferedOutputStream是一个线程安全的类。
第二:BufferedOutputStream是采用装饰模式,通过将写入数据缓存起来,之后当达到缓存最大值,将数据flush()到指定OutputStream,这样避免多次写,因为每次调用write()开销比较大,特别每次调用write写入一个字节时,开销特别大。而调用System.arraycopy()可以节省时间,提高效率。
第三:BufferedOutputStream结束时,一定要flush(),因为大多数情况下,一些数据还在缓存中,需要结束时将数据flush()到指定的outputstream,不然将会丢失数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值