Difference between ByteArrayOutputStream and BufferedOutputStream
java, stream
Solution
Just look at the javadoc:
ByteArrayOutputStream:
This class implements an output stream in which the data is written into a byte array.
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.
Problem
Both `ByteArrayOutputStream` and `BufferedOutputStream` do buffering by placing data in an array in memory. So my questions are - what are the differences between these two. - When to use `ByteArrayOutputStream` and when to use `BufferedOutputStream` Can some one help me in above two questions as i am confused on this.