joe_pool_is
Contributor
What are the Pros and Cons with performing a stream operation in chunks as opposed to performing that same operation on the full file?
"In Chunks" Example:
"Full File" Example:
I just want to make sure I use a technique that will give me the greatest results while not putting my code into a dangerous situation.
I'm guessing the ReadFileAtOnce method works fine as long as there is enough RAM to read the entire file. Passing ReadFileAtOnce a 6GB ZIP file backup of a DVD would likely cause the method to fail.
Any other thoughts on the subject?
"In Chunks" Example:
Code:
void ReadFileInChunks(string file) {
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) {
int len;
byte[] buffer = new byte[1024];
do {
len = fs.Read(buffer, 0, 1024);
Console.WriteLine("Read 1024 bytes of data.");
} while (0 < len);
fs.Close();
}
}
Code:
void ReadFileAtOnce(string file) {
using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read)) {
byte[] buffer = new byte[fs.Length];
if (fs.Read(buffer, 0, buffer.Length) == buffer.Length) {
Console.WriteLine("Finished!");
}
fs.Close();
}
}
I'm guessing the ReadFileAtOnce method works fine as long as there is enough RAM to read the entire file. Passing ReadFileAtOnce a 6GB ZIP file backup of a DVD would likely cause the method to fail.
Any other thoughts on the subject?