Stream Reuse in C#

Posted by MikeD on Stack Overflow See other posts from Stack Overflow or by MikeD
Published on 2009-06-10T03:54:15Z Indexed on 2010/04/23 3:53 UTC
Read the original article Hit count: 389

Filed under:

I've been playing around with what I thought was a simple idea. I want to be able to read in a file from somewhere (website, filesystem, ftp), perform some operations on it (compress, encrypt, etc.) and then save it somewhere (somewhere may be a filesystem, ftp, or whatever). It's a basic pipeline design. What I would like to do is to read in the file and put it onto a MemoryStream, then perform the operations on the data in the MemoryStream, and then save that data in the MemoryStream somewhere. I was thinking I could use the same Stream to do this but run into a couple of problems:

  1. Everytime I use a StreamWriter or StreamReader I need to close it and that closes the stream so that I cannot use it anymore. That seems like there must be some way to get around that.
  2. Some of these files may be big and so I may run out of memory if I try to read the whole thing in at once.

I was hoping to be able to spin up each of the steps as separate threads and have the compression step begin as soon as there is data on the stream, and then as soon as the compression has some compressed data available on the stream I could start saving it (for example). Is anything like this easily possible with the C# Streams? ANyone have thoughts as to how to accomplish this best?

Thanks,

Mike

© Stack Overflow or respective owner

Related posts about c#