Reputation: 14065
I am trying to download a file from SharePoint. After making some research,I got this and it says that it will be better for the performance if we use buffer.
Remark - file is SPFile
using (System.IO.Stream strm = file.OpenBinaryStream())
{
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead;
do
{
bytesRead = strm.Read(buffer, 0, BUFFER_SIZE);
response.OutputStream.Write(buffer, 0, bytesRead);
response.Flush();
} while (bytesRead > 0);
}
Once we assign like response.BinaryWrite(file.OpenBinary());
, are we getting the whole stream in strm object (start consuming RAM) ? Let's say the file is 10MB , then this strm will be 10 MB in RAM ?
Or it will start consuming memory once we start reading ? bytesRead = strm.Read(buffer, 0, BUFFER_SIZE);
Upvotes: 2
Views: 519
Reputation: 1062550
A Stream
is a pipe, not a bucket; it doesn't (usually) "contain" the data - it just manages access to the data. In terms of the example, the data loaded by your app at any time is BUFFER_SIZE
bytes (plus any other buffers used by any other layers).
You also don't really need to Flush()
on every write (although a Flush()
at the end might be appropriate).
What you have is fine; the only thing I'd say is that you can simplify this in recent version of .NET:
using (System.IO.Stream strm = file.OpenBinaryStream())
{
strm.CopyTo(response);
// response.Flush(); // optional: only included because it is in the question
}
Upvotes: 3