JDK 源码阅读笔记(一)--OutputStream

来源:互联网 发布:linux怎么运行脚本 编辑:程序博客网 时间:2024/06/05 21:07

FileOutputStream类

1.一个文件可以由多个FileOutputStream打开,这个特性跟具体的操作系统有关。

2.一个流关闭,文件并不一定关闭

3.不同流可向文件中写数据,这部分同步控制由底层控制,程序员可以不管。

这三个特性是由FileDescriptor保证的。当这个文件新创建了一个流时,计数器加一。

当一个流关闭时,FD计数器减一,这时还要看这个流的channel是否为null,如果不为null还要关闭channel,同时还得让FD计数器减一。

当FD计数器减为0时,并且isRunningFinalize(),则真正关闭文件。

BufferedOutputStream重要的方法,看英文注释。

    public synchronized void write(byte b[], int off, int len) throws IOException {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. */    flushBuffer();    out.write(b, off, len);    return;}if (len > buf.length - count) {    flushBuffer();}System.arraycopy(b, off, buf, count, len);count += len;    }