Bhuban Bhuban - 1 year ago 86
ASP.NET (C#) Question

Memory Stream vs File Stream for static content download

I have a scenario that every file uploaded that may be of any MIME type should be encrypted and when use wants to download they should be decrypted.

For this I have decrypted a requested file and saved that file in a temporary location.
My decryption method writes to a filestream by reading encrypted filestream.

Now should I change my algorithm to save encrypted filestream to a memory stream
and download directly from memory stream instead of writing to filestream and downloading that file.

In term of performance which would be better filestream or memory stream in this case.

I am thinking that if multiple huge file is requested by multiple users lets say 100 different files are requested by 100 different users. In this case memory may run out and we may face some unwanted troubles.

Which one I should implement.

Answer Source

Have you considered streaming directly into the NetworkStream for output? You will need no memory and no disc space for that. If you expect higher performance from buffering the data in a BufferedStream as an intermediate.

If direct or buffered streaming is not an option for you, the correct answer depends on your requirements. You need to know the size of your files and the expected umber of requests. If you think your web servers memory can handle it and you need the added performance, go for it. Otherwise you should buffer on the hard drive. Remember: you only need sufficient performance, not perfect performance.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download