Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Case 1: use ByteArrayOutputStream to create a file and download it.

  • Case 2: use FileOutputStream to temporarily create a file under web server and download it and then delete this file.

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.

like image 654
swemon Avatar asked Nov 02 '22 17:11

swemon


1 Answers

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[]

like image 124
TomerZ Avatar answered Nov 12 '22 21:11

TomerZ