[英]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. 我想要做的是从Web服务器下载文件。 When I traced the code, two programmers uses ByteArrayOutputStream and FileOutputStream differently to download file in the same scenario.
当我跟踪代码时,两个程序员以不同的方式使用ByteArrayOutputStream和FileOutputStream来下载同一场景中的文件。 These are
这些是
Case 1 : use ByteArrayOutputStream
to create a file and download it. 案例1 :使用
ByteArrayOutputStream
创建文件并下载。
Case 2 : use FileOutputStream
to temporarily create a file under web server and download it and then delete this file. 案例2 :使用
FileOutputStream
在Web服务器下临时创建文件并下载然后删除该文件。
PS: Case 2 file is larger than case 1 file. PS:案例2文件大于案例1文件。
Can I use ByteArrayOutputStream
to both cases? 我可以在两种情况下使用
ByteArrayOutputStream
吗? Is there any intention to use FileOutputStream
in second case? 是否有意在第二种情况下使用
FileOutputStream
? 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. 结合Boris The Spider和Peter Lawrey的回答:
ByteArrayOutputStream
在内存中, FileOutputStream
是一个文件。 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. ByteArrayOutputStream
更快但考虑下载一个10Gb文件......这似乎在程序中打开了一个很好的安全漏洞 - 只需要输入一个大文件。 Also ByteArrayOutputStream
is limited to just under 2GB as it uses a byte[] ByteArrayOutputStream
也限制在2GB以下,因为它使用了一个byte []
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.