swemon
swemon

Reputation: 5946

ByteArrayOutputStream vs FileOutputStream from memory usage and performance point of view

What I want to do is to download a file from the web server. When I traced the code, two programmers uses ByteArrayOutputStream and FileOutputStream differently to download file in the same scenario. These are

PS: Case 2 file is larger than case 1 file.

Can I use ByteArrayOutputStream to both cases? Is there any intention to use FileOutputStream in second case? What I want to know is from performance and memory point of view. Thanks in advance.

Upvotes: 6

Views: 4725

Answers (1)

TomerZ
TomerZ

Reputation: 635

Combining Boris The Spider's and Peter Lawrey's to an answer: ByteArrayOutputStream is in memory and FileOutputStream is a file. The implications are obvious. ByteArrayOutputStream is faster but consider downloading a 10Gb file... This would seem to open a nice security hole in the program - just feed it a large file. Also ByteArrayOutputStream is limited to just under 2GB as it uses a byte[]

Upvotes: 3

Related Questions