Difference between ByteArrayOutputStream and BufferedOutputStream
Asked Answered
R

3

9

Both ByteArrayOutputStream and BufferedOutputStream do buffering by placing data in an array in memory. So my questions are

  1. what are the differences between these two.
  2. When to use ByteArrayOutputStream and when to use BufferedOutputStream

Can some one help me in above two questions as i am confused on this.

Reason answered 6/5, 2017 at 9:2 Comment(5)
Thaht question is quite different it is saying "Is there any advantage in wrapping a BufferedOutputStream around a ByteArrayOutputStream instead of just using the ByteArrrayOutputStream by itself?" I read this question but nothing please check againReason
Only diff i found on interner is BufferedOutputStream writes the data to another stream. ByteArrayOutputStream writes the data to a byte array.Reason
Can you please explain this please ???Reason
Thanks a lot @GhostCat and jesper..appreciate efforts you put...(Y)Reason
Just wondering: you are waiting for more answers before accepting some answer here? My gut feeling is that you won't hear much else..Henkel
H
9

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.

Henkel answered 6/5, 2017 at 9:17 Comment(0)
F
9

ByteArrayOutputStream writes bytes to a byte array in memory. 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.

BufferedOutputStream wraps another, underlying OutputStream and provides buffering for that underlying stream, to make I/O operations more efficient. The underlying stream can be any kind of OutputStream, for example one that writes to a file or a network socket.

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.

Forde answered 6/5, 2017 at 9:16 Comment(0)
H
9

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.

Henkel answered 6/5, 2017 at 9:17 Comment(0)
P
1

The BufferedOutputStream allows to improve performance by using buffer. 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. The next calls of the write() method of BufferedOutputStream will store bytes in buffer until it filled again.

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.

Praetorian answered 25/7, 2018 at 5:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.