Re: Streaming Techniques
- From: "Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Wed, 3 Dec 2008 11:06:59 -0500
Well, first off, your examples are wrong. You are not guaranteed to get
the number of bytes back that you request, and should account for that in
your code.
That being said, if you have a very large file, it would probably be a
little prohibitive to load all of the data into memory. I'm being ambiguous
with terms like "very large file" and "prohibitive" because it depends on
the specs of your machine, the code, etc, etc. You will have to test to
determine which is best.
Reading the entire file if the file is 6GB probably would be fine on a
64 bit machine, but on a 32 bit machine, you are going to run into definite
issues.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"jp2msft" <jp2msft@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:DA301DE8-9211-407F-8E10-6F0C7FBFBAB1@xxxxxxxxxxxxxxxx
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?
.
- Follow-Ups:
- Re: Streaming Techniques
- From: jp2msft
- Re: Streaming Techniques
- References:
- Streaming Techniques
- From: jp2msft
- Streaming Techniques
- Prev by Date: Replace characters.
- Next by Date: Re: Comparing two values - vs2005
- Previous by thread: Streaming Techniques
- Next by thread: Re: Streaming Techniques
- Index(es):
Relevant Pages
|