J
jp2msft
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:
// *************************
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();
}
}
"Full File" Example:
// *************************
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 want to use a technique that will give me the greatest performance while
ensuring the method does not fail.
I'm guessing the ReadFileAtOnce method works fine as long as there is enough
RAM to read the entire file. (i.e. Passing ReadFileAtOnce a 6GB ZIP file
backup of a DVD would be bad.)
Any other thoughts on the subject?
opposed to performing that same operation on the full file?
"In Chunks" Example:
// *************************
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();
}
}
"Full File" Example:
// *************************
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 want to use a technique that will give me the greatest performance while
ensuring the method does not fail.
I'm guessing the ReadFileAtOnce method works fine as long as there is enough
RAM to read the entire file. (i.e. Passing ReadFileAtOnce a 6GB ZIP file
backup of a DVD would be bad.)
Any other thoughts on the subject?