所有 Java 输出流都有缓冲区吗?
Do all Java output streams have buffers?
我用了 OutputStreamWriter
套接字。当我尝试写入数据时,发现数据并没有发送出去。所以我尝试了flush()
,它成功了。
我想请教一些问题:
- 是否所有输出流都有缓冲区?那么像
BufferedOutputStream
这样的 class 有什么独特的好处吗? (因为有缓冲区)
- 当我使用
close()
时。缓冲区中的数据是否消失或被冲出?我测试了一下,buffer中的数据没有成功写出。但是我在很多地方看到调用close()
会刷新缓冲区中的数据?
并非所有 OutputStream
实现都有缓冲区。
这很容易证明,因为您可以自己实现。
但是 OutputStream
的大多数用途通常会在某种程度上涉及缓冲区 。
BufferedOutputStream
是明显的候选者,其中缓冲区直接位于 class 中作为 class 的主要特征。通过这种方式,您可以向任何其他 OutputStream
.
添加显式缓冲
OutputStream
s 与 OS 通信,例如 FileOutputStream
或为网络连接返回的 OutputStream
本身没有显式缓冲区,但会写入OS 可能有也可能没有自己的缓冲区(例如,大多数 OS 不会在 write
系统调用时立即写入文件,只会在关闭时或在一些之后刷新时间)。
在任何一种情况下(OutputStream
中的直接缓冲区或底层系统缓冲区)flush
调用往往会启动对“真实”目标的回写。
请注意,在符合条件的 OutputStream
上,收盘前的 flush
永远不需要。
并非所有 OutputStream
实现都执行缓冲,尽管缓冲是一个很常见的特性,它们将 flush()
方法定义为 OutputStream
契约的一部分(特别是通过扩展Flushable
界面)。我认为最好的信息来源是 Javadoc of that method:
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
The flush
method of OutputStream
does nothing.
我已经突出显示了很明显缓冲是否实现取决于实现的部分。
我用了 OutputStreamWriter
套接字。当我尝试写入数据时,发现数据并没有发送出去。所以我尝试了flush()
,它成功了。
我想请教一些问题:
- 是否所有输出流都有缓冲区?那么像
BufferedOutputStream
这样的 class 有什么独特的好处吗? (因为有缓冲区) - 当我使用
close()
时。缓冲区中的数据是否消失或被冲出?我测试了一下,buffer中的数据没有成功写出。但是我在很多地方看到调用close()
会刷新缓冲区中的数据?
并非所有 OutputStream
实现都有缓冲区。
这很容易证明,因为您可以自己实现。
但是 OutputStream
的大多数用途通常会在某种程度上涉及缓冲区 。
BufferedOutputStream
是明显的候选者,其中缓冲区直接位于 class 中作为 class 的主要特征。通过这种方式,您可以向任何其他 OutputStream
.
OutputStream
s 与 OS 通信,例如 FileOutputStream
或为网络连接返回的 OutputStream
本身没有显式缓冲区,但会写入OS 可能有也可能没有自己的缓冲区(例如,大多数 OS 不会在 write
系统调用时立即写入文件,只会在关闭时或在一些之后刷新时间)。
在任何一种情况下(OutputStream
中的直接缓冲区或底层系统缓冲区)flush
调用往往会启动对“真实”目标的回写。
请注意,在符合条件的 OutputStream
上,收盘前的 flush
永远不需要。
并非所有 OutputStream
实现都执行缓冲,尽管缓冲是一个很常见的特性,它们将 flush()
方法定义为 OutputStream
契约的一部分(特别是通过扩展Flushable
界面)。我认为最好的信息来源是 Javadoc of that method:
Flushes this output stream and forces any buffered output bytes to be written out. The general contract of flush is that calling it is an indication that, if any bytes previously written have been buffered by the implementation of the output stream, such bytes should immediately be written to their intended destination.
If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive.
The
flush
method ofOutputStream
does nothing.
我已经突出显示了很明显缓冲是否实现取决于实现的部分。