简体   繁体   English

BufferedOutputStream的目的是什么?

[英]what's the purpose of BufferedOutputStream?

我想知道BufferedOutputStream的用途,使用它时的性能提升?

Here is the line from API of BufferedOutputStream : 以下是来自BufferedOutputStream的API的行:

The class implements a buffered output stream. 该类实现缓冲输出流。 By setting up such an output stream, an application can write bytes to the underlying output stream without necessarily causing a call to the underlying system for each byte written. 通过设置这样的输出流,应用程序可以将字节写入底层输出流, 不必为写入的每个字节调用底层系统。

It can do most of operations within the buffer, and without a call to the underlying system. 它可以在缓冲区内完成大部分操作,而无需调用底层系统。

For example, consider writing to file: without buffer, it has to make a system call for every single byte, which is obviously slow. 例如,考虑写入文件:没有缓冲区,它必须为每个字节进行系统调用,这显然很慢。

As its name suggests, BufferOutputStream has an internal buffer ( byte[] ) to which contents of individual small writes are first copied. 顾名思义, BufferOutputStream有一个内部缓冲区( byte[] ),首先复制各个小写入的内容。 They are written to the underlying OutputStream when buffer is full, or the stream is flushed, or the stream is closed. 当缓冲区已满或刷新流或关闭流时,它们将写入底层的OutputStream This can make a big difference if there is a (relatively large) fixed overhead for each write operation to the underlying OutputStream , as is the case for FileOutputStream (which must make an operating system call) and many compressed streams. 如果对于底层OutputStream每次写入操作都存在(相对较大的)固定开销,这可能会产生很大的不同,就像FileOutputStream (必须进行操作系统调用)和许多压缩流的情况一样。

At the same time, many stream-based libraries use their own buffering (like XML and JSON writers), and use of BufferedOutputStream provides no benefit. 同时,许多基于流的库使用自己的缓冲(如XML和JSON编写器),并且使用BufferedOutputStream没有任何好处。 But its own overhead is relatively low so there isn't much risk. 但它自己的开销相对较低,因此风险不大。

BufferedOutputStream提供输出数据缓冲,通过存储要写入缓冲区的值并在缓冲区填充或调用flush()方法时实际写出来提高效率。

Java BufferedOutputStream class is used for buffering an output stream. Java BufferedOutputStream类用于缓冲输出流。 It internally uses buffer to store data. 它在内部使用缓冲区来存储数据。 It adds more efficiency than to write data directly into a stream. 与将数据直接写入流中相比,它提高了效率。 So, it makes the performance fast. 因此,它使性能快速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM