简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM