简体   繁体   中英

what's the purpose of BufferedOutputStream?

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

Here is the line from API of BufferedOutputStream :

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. They are written to the underlying OutputStream when buffer is full, or the stream is flushed, or the stream is closed. 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.

At the same time, many stream-based libraries use their own buffering (like XML and JSON writers), and use of BufferedOutputStream provides no benefit. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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