简体   繁体   English

ByteArrayOutputStream和BufferedOutputStream之间的区别

[英]Difference between ByteArrayOutputStream and BufferedOutputStream

Both ByteArrayOutputStream and BufferedOutputStream do buffering by placing data in an array in memory. ByteArrayOutputStreamBufferedOutputStream都通过将数据放入内存中的数组来进行缓冲。 So my questions are 所以我的问题是

  1. what are the differences between these two. 这两者之间有什么不同。
  2. When to use ByteArrayOutputStream and when to use BufferedOutputStream 何时使用ByteArrayOutputStream以及何时使用BufferedOutputStream

Can some one help me in above two questions as i am confused on this. 有人可以帮助我解决上面两个问题,因为我对此感到困惑。

Just look at the javadoc: 看看javadoc:

ByteArrayOutputStream : ByteArrayOutputStream

This class implements an output stream in which the data is written into a byte array. 此类实现一个输出流,其中数据被写入字节数组。

BufferedOutputStream : 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. 通过设置这样的输出流,应用程序可以将字节写入底层输出流,而不必为写入的每个字节调用底层系统。

So, those are really two very different things: 所以,那些是非常不同的两件事:

  • the first one you use when you know that you have some data that in the end you need as array of bytes 当你知道你有一些数据最终你需要作为字节数组时,你使用的第一个
  • the second one is just a wrapper around any other kind of output stream - which adds buffering. 第二个只是任何其他类型的输出流的包装 - 它增加了缓冲。

That is all there is to this! 这就是全部!

And if you want to experience a different behavior: create a buffered one that writes to a file, and an array one. 如果您想体验不同的行为:创建一个写入文件的缓冲行为和一个数组。 Then just keep pushing bytes into each one. 然后继续将字节推入每个字节。 The array one will cause a memory problem at some point, the other one might not stop until all of your disk space is used up. 数组1会在某个时刻导致内存问题,另一个可能会停止,直到所有磁盘空间都用完为止。

ByteArrayOutputStream writes bytes to a byte array in memory. ByteArrayOutputStream将字节写入内存中的字节数组。 Not to any other destination, such as a file or a network socket. 不到任何其他目的地,例如文件或网络套接字。 After writing the data, you can get the byte array by calling toByteArray() on it. 写入数据后,可以通过调用toByteArray()来获取字节数组。

BufferedOutputStream wraps another, underlying OutputStream and provides buffering for that underlying stream, to make I/O operations more efficient. BufferedOutputStream包装另一个底层OutputStream并为该底层流提供缓冲,以提高I / O操作的效率。 The underlying stream can be any kind of OutputStream , for example one that writes to a file or a network socket. 底层流可以是任何类型的OutputStream ,例如写入文件或网络套接字的OutputStream

Why you might want to use buffering: Writing a large block of data to the file system is more efficient than writing byte by byte. 为什么要使用缓冲:将大块数据写入文件系统比逐字节写入更有效。 If your program needs to write many small pieces of data, it's more efficient to first gather these small pieces in a buffer and then write the entire buffer to disk at once. 如果您的程序需要编写许多小块数据,那么首先在缓冲区中收集这些小块然后立即将整个缓冲区写入磁盘会更有效。 This is what BufferedOutputStream does automatically for you. 这就是BufferedOutputStream为您自动执行的操作。

The BufferedOutputStream allows to improve performance by using buffer. BufferedOutputStream允许使用缓冲区来提高性能。 When the buffer fills up, calling the write() method causes to underlying output stream write() method call, and the contents of the buffer are written to destination. 当缓冲区填满时,调用write()方法会导致基础输出流write()方法调用,并且缓冲区的内容将写入目标。 The next calls of the write() method of BufferedOutputStream will store bytes in buffer until it filled again. BufferedOutputStreamwrite()方法的下一次调用将在缓冲区中存储字节,直到它再次填充。

Usually used as wrapper, for example: 通常用作包装器,例如:

FileOutputStream fos = new FileOutputStream("file.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
bos.write( ... );

Thus, the number of calls of the underlying operating system functions is minimized. 因此,最小化了底层操作系统功能的调用次数。

The ByteArrayOutputStream allows to write the stream of bytes to the array of bytes. ByteArrayOutputStream允许将字节流写入字节数组。

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

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