21

Is there any advantage in wrapping a BufferedOutputStream around a ByteArrayOutputStream instead of just using the ByteArrrayOutputStream by itself?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Hank
  • 3,367
  • 10
  • 48
  • 86

3 Answers3

20

Generally BufferedOutputStream wrapper is mostly used to avoid frequent disk or network writes. It can be much more expensive to separately write a lot of small pieces than make several rather large operations. The ByteArrayOutputStream operates in memory, so I think the wrapping is pointless.

If you want to know the exact answer, try to create a simple performance-measuring application.

Binus
  • 1,065
  • 7
  • 14
2

Absolutely none. Though BufferedWriter and BufferedReader do offer extra functionality were you to be operating on strings.

Dunes
  • 37,291
  • 7
  • 81
  • 97
0

ByteArrayOutputStream is not recommended if you want to get high performance, but one interesting feature is to send a message with unknown length. For a better comprehension about how these two methods work, see http://java-performance.info/java-io-bytearrayoutputstream/.

DrLuck
  • 1
  • 1