Stream Reuse in C#

前端 未结 3 1193
梦毁少年i
梦毁少年i 2021-01-18 01:11

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

相关标签:
3条回答
  • 2021-01-18 01:44

    StreamReader/StreamWriter shouldn't have been designed to close their underlying stream -- that's a horrible misfeature in the BCL. But they do, they won't be changed (because of backward compatibility), so we're stuck with this disaster of an API.

    But there are some well-established workarounds, if you want to use StreamReader/Writer but keep the Stream open afterward.

    • For a StreamReader: don't Dispose the StreamReader. It's that simple. It's harmless to just let a StreamReader go without ever calling Dispose. The only effect is that your Stream won't get prematurely closed, which is actually a plus.
    • For a StreamWriter: there may be buffered data, so you can't get away with just letting it go. You have to call Flush, to make sure that buffered data gets written out to the Stream. Then you can just let the StreamWriter go. (Basically, you put a Flush where you normally would have put a Dispose.)
    0 讨论(0)
  • 2021-01-18 01:54

    Unless you're reading in streams bigger than your hard drive, I don't think you'll run out of memory:

    http://blogs.msdn.com/ericlippert/archive/2009/06/08/out-of-memory-does-not-refer-to-physical-memory.aspx

    0 讨论(0)
  • 2021-01-18 01:55

    Using a helper method to drive the streaming:

    static public void StreamCopy(Stream source, Stream target)
    {
        byte[] buffer = new byte[8 * 1024];
    
        int size;
        do
        {
          size = source.Read(buffer, 0, 8 * 1024);
          target.Write(buffer, 0, size);
        } while (size > 0);
    }
    

    You can easily combine whatever you need:

    using (FileStream iFile = new FileStream(...))
    using (FileStream oFile = new FileStream(...))
    using (DeflateStream oZip = new DeflateStream(outFile, CompressionMode.Compress))
        StreamCopy(iFile, oZip);
    

    Depending on what you are actually trying to do, you'd chain the streams differently. This also uses relatively little memory, because only the data being operated upon is in memory.

    0 讨论(0)
提交回复
热议问题