Re: Streaming Techniques

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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?


.



Relevant Pages

  • Re: Streaming Techniques
    ... opposed to performing that same operation on the full file? ... void ReadFileInChunks{ ... using (FileStream fs = new FileStream(file, FileMode.Open, ... I'm guessing the ReadFileAtOnce method works fine as long as there is ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: regarding free function in c library
    ... pointers are typecasted to void* in many places before performing ... people are stupid ... people are using a C++ compiler ...
    (comp.lang.c)
  • Re: Terminate a Thread
    ... If the process performs a task you do not want it to, check the cancel ... flag before performing any such task: ... void doStepTwo{ ...
    (comp.lang.java.programmer)
  • Streaming Techniques
    ... What are the "Pros and Cons" with performing a stream operation in chunks as ... "In Chunks" Example: ... I'm guessing the ReadFileAtOnce method works fine as long as there is enough ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Durham
    ... The Void wrote: ... Ewano wrote: ... knew who was performing I might be more up for coming up. ... I heartily reccomend going to a juggling convention on your birthday. ...
    (rec.juggling)