5

What's the most efficient way to read a stream into another stream? In this case, I'm trying to read data in a Filestream into a generic stream. I know I could do the following:
1. read line by line and write the data to the stream
2. read chunks of bytes and write to the stream
3. etc

I'm just trying to find the most efficient way.

Thanks

cynicalman
  • 5,861
  • 3
  • 30
  • 30
James
  • 479
  • 1
  • 7
  • 19
  • What do you mean by efficient? The was that is the fastest, uses the least amount of memory, or some other criteria? – Jeff Stong Sep 24 '08 at 23:50

4 Answers4

7

Stephen Toub discusses a stream pipeline in his MSDN .NET matters column here. In the article he describes a CopyStream() method that copies from one input stream to another stream. This sounds quite similar to what you're trying to do.

Jeff Stong
  • 1,506
  • 4
  • 14
  • 26
  • I really liked that article for the parallel tasks perspective, but that stream copy is pretty useful in its own right. However, if the stream does support the Length property (if CanSeek is false, exception), the buffer should be the length of the input stream and be done in a single read. – Jesse C. Slicer Sep 24 '08 at 20:27
7

I rolled together a quick extension method (so VS 2008 w/ 3.5 only):

public static class StreamCopier
{
   private const long DefaultStreamChunkSize = 0x1000;

   public static void CopyTo(this Stream from, Stream to)
   {
      if (!from.CanRead || !to.CanWrite)
      {
         return;
      }

      var buffer = from.CanSeek
         ? new byte[from.Length]
         : new byte[DefaultStreamChunkSize];
      int read;

      while ((read = from.Read(buffer, 0, buffer.Length)) > 0)
      {
        to.Write(buffer, 0, read);
      }
   }
}

It can be used thus:

 using (var input = File.OpenRead(@"C:\wrnpc12.txt"))
 using (var output = File.OpenWrite(@"C:\wrnpc12.bak"))
 {
    input.CopyTo(output);
 }

You can also swap the logic around slightly and write a CopyFrom() method as well.

Jesse C. Slicer
  • 19,901
  • 3
  • 68
  • 87
1

Reading a buffer of bytes and then writing it is fastest. Methods like ReadLine() need to look for line delimiters, which takes more time than just filling a buffer.

Kent Boogaart
  • 175,602
  • 35
  • 392
  • 393
0

I assume by generic stream, you mean any other kind of stream, like a Memory Stream, etc.

If so, the most efficient way is to read chunks of bytes and write them to the recipient stream. The chunk size can be something like 512 bytes.

Chris Wenham
  • 23,679
  • 13
  • 59
  • 69