源码剖析之java.io.BufferedOutputStream

与缓冲输入流java.io.BufferedInputStream 对应的输出缓冲流为:java.io.BufferedOutputStream

功能是缓冲输出。再次强调缓冲的本质是:byte数组操作。

源码分析如下:


package java.io;

/**
* 缓冲输出流。实现此类,必须提供一个underlying output stream。
特点:通过BufferedOutputStream 装饰后的流,每次写操作不一定都会反映到
underlying output stream,会在超出buf缓冲区后才flush的。
*/
public class BufferedOutputStream extends FilterOutputStream {
/**
* 数据存储的内部缓冲区
*/
protected byte buf[];

/**
buf中有效的字节数。
count区间:[0,buf.length]
有效数据:buf[0] 到 buf[count-1]
*/
protected int count;

/**
*构造函数,默认的缓冲为8M
*/
public BufferedOutputStream(OutputStream out) {
this(out, 8192);
}

/**
*同上
*/
public BufferedOutputStream(OutputStream out, int size) {
super(out);
if (size <= 0) {
throw new IllegalArgumentException("Buffer size <= 0");
}
buf = new byte[size];
}

/**
清刷内部缓冲的数据,就是把buf中的数据全部写入 underlying output stream.
*/
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}

/**
* 写入一个字节
*/
public synchronized void write(int b) throws IOException {
if (count >= buf.length) { //只有buf满,才会清刷缓冲区
flushBuffer();
}
buf[count++] = (byte)b; //如果缓冲区不满,直接放入缓冲区即可
}

/**
* 批量写入数据
*/
public synchronized void write(byte b[], int off, int len) throws IOException {
if (len >= buf.length) { //如果要写入的数据大于缓冲区,那么直接写入underlying output stream ,不需要写入缓冲区buf了。
flushBuffer();
out.write(b, off, len);
return;
}
if (len > buf.length - count) { //如果len大于 缓冲区剩余的空间,那么需要先清刷数据
flushBuffer();
}
System.arraycopy(b, off, buf, count, len); //把b 从off 起的数据复制到buf中,位置为count,长度为len
count += len; //count值新增len
}

/**
* 立即刷新缓冲区。第一步要保证缓冲区的数据刷新,并且underlying output stream 也要立即刷新
*/
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
}



[b]提醒:源码分析依然是理解jdk包功能本质的最有效途径~[/b]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值