BufferedOutputStream 源码分析
来源:互联网 发布:起点写作软件 编辑:程序博客网 时间:2024/05/18 22:51
转自:
BufferedOutputStream 是缓冲输出流。它继承于FilterOutputStream。
BufferedOutputStream 的作用是为另一个输出流提供“缓冲功能”。
public class BufferedOutputStream extends FilterOutputStream { // 保存“缓冲输出流”数据的字节数组 protected byte buf[]; // 缓冲中数据的大小 protected int count; // 构造函数:新建字节数组大小为8192的“缓冲输出流” public BufferedOutputStream(OutputStream out) { this(out, 8192); } // 构造函数:新建字节数组大小为size的“缓冲输出流” public BufferedOutputStream(OutputStream out, int size) { super(out); if (size <= 0) { throw new IllegalArgumentException("Buffer size <= 0"); } buf = new byte[size]; } // 将缓冲数据都写入到输出流中 private void flushBuffer() throws IOException { if (count > 0) { out.write(buf, 0, count); count = 0; } } // 将“数据b(转换成字节类型)”写入到输出流中 public synchronized void write(int b) throws IOException { // 若缓冲已满,则先将缓冲数据写入到输出流中。 if (count >= buf.length) { flushBuffer(); } // 将“数据b”写入到缓冲中 buf[count++] = (byte)b; } public synchronized void write(byte b[], int off, int len) throws IOException { // 若“写入长度”大于“缓冲区大小”,则先将缓冲中的数据写入到输出流,然后直接将数组b写入到输出流中 if (len >= buf.length) { flushBuffer(); out.write(b, off, len); return; } // 若“剩余的缓冲空间 不足以 存储即将写入的数据”,则先将缓冲中的数据写入到输出流中 if (len > buf.length - count) { flushBuffer(); } System.arraycopy(b, off, buf, count, len); count += len; } // 将“缓冲数据”写入到输出流中 public synchronized void flush() throws IOException { flushBuffer(); out.flush(); } }
说明:BufferedOutputStream的源码非常简单,这里就BufferedOutputStream的思想进行简单说明:BufferedOutputStream通过字节数组来缓冲数据,当缓冲区满或者用户调用flush()函数时,它就会将缓冲区的数据写入到输出流中。
0 0
- BufferedOutputStream 源码分析
- BufferedOutputStream 源码分析
- [Java]BufferedOutputStream、DataInputStream源码分析
- Java IO:BufferedOutputStream使用详解及源码分析
- BufferedOutputStream 源码的个人理解
- (四)BufferedOutputStream源码学习
- BufferedOutputStream
- BufferedOutputStream
- JDK的java.io.BufferedOutputStream的源码
- Java8 I/O源码-BufferedInputStream与BufferedOutputStream
- BufferedOutputStream的缓存功能解析(源码阅读)
- BufferedInputStream、BufferedOutputStream
- BufferedInputStream&BufferedOutputStream
- BufferedInputStream/BufferedOutputStream
- BufferedOutputStream下载
- IO BufferedOutputStream
- Java_io体系之BufferedInputStream、BufferedOutputStream简介、走进源码及示例——10
- java io系列13之 BufferedOutputStream(缓冲输出流)的认知、源码和示例
- ARM汇编
- 分段录制的实现
- 编译安装glibc
- cocos2d-x在eclipse上搭建开发环境
- 10款设计精巧的jQuery/CSS3插件
- BufferedOutputStream 源码分析
- [QT]——Qt 常用命令,宏,pro文件格式
- 驗證漢字的正則表達式
- Delphi实现RGB色环的代码绘制
- DataInputStream 源码分析
- java读取tiff格式图片信息的方式。
- Ubuntu每次启动都显示System program problem detected的解决办法
- 元表metatable
- Java 枚举7常见种用法