什么时候使用字节数组,什么时候使用流? [英] When to use byte array, and when to use stream?
问题描述
我需要将图像和小的视频文件(大约5MB,小于10MB)发送到我将要编写的REST服务. 我想知道是否应该使用Byte []或Stream来完成此任务. 在使用Byte []和Stream之间的传输文件大小上的分界线是什么?
I need to send images and small video files (around 5MB, less than 10MB) to a REST service, which I will write. I am wondering whether I should use Byte[] or Stream to accomplish this task. What would be the dividing line in terms of transfer file size between using Byte[] and Stream?
推荐答案
您愿意承诺用于事务的可用内存量是您唯一的实际限制.
The amount of free memory you're willing to commit to the transaction is your only real constraint.
如果文件大小为5M,则需要将整个文件加载到RAM中,这将花费5M.
If you have a 5M file, then you'll need to load the entire thing in to RAM, which will cost 5M.
如果对其进行流处理,则可以通过将文件中的小块读取到可重用的缓冲区中并将这些块写入HTTP流中来使用更少的内存.
If you stream it, you can use far less memory, by reading small chunks from the file in to a reusable buffer and writing those chunks to the HTTP stream.
这篇关于什么时候使用字节数组,什么时候使用流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!