PumpStream

PumpStream is a handy C# static method for moving (or "pumping") all the bytes from one stream to another.

It's helpful when you want to move data from a FileStream into a DeflateStream, for example. Another good use is streaming data from a MemoryStream or FileStream to a client via ASP.NET's Response.OutputStream property.

Since PumpStream uses a configurable size intermediate buffer, you're never going to run out of memory no matter how much data you're pumping.

Download
Code
  1 // Copyright 2003-2007 Josh Christie
  2
// http://www.joshchristie.com
  3
  4
using System;
  5
using System.Collections;
  6
using System.IO;
  7
  8
namespace JC.Utility {
  9     
public static class IOHelper {
 10         
/// <summary>
 11         
/// Pumps all data from the input stream to the output stream using an intermediate buffer.
 12         
/// </summary>
 13         
/// <remarks>
 14         
/// The input stream is closed by this method.
 15         
/// </remarks>
 16         
/// <param name="input">Stream to read from.</param>
 17         
/// <param name="output">Stream to write to.</param>
 18         
/// <returns>Number of bytes pumped from input stream to output stream.</returns>
 19         
public static int PumpStream(Stream input, Stream output) {
 20             
return PumpStream(input, output, 8192);
 21         
}
 22
 23         
/// <summary>
 24         
/// Pumps all data from the input stream to the output stream using an intermediate buffer of specified size.
 25         
/// </summary>
 26         
/// <remarks>
 27         
/// The input stream is closed by this method.
 28         
/// </remarks>
 29         
/// <param name="input">Stream to read from.</param>
 30         
/// <param name="output">Stream to write to.</param>
 31         
/// <param name="pumpBufferSize">Size in bytes of the intermediate buffer.</param>
 32         
/// <returns>Number of bytes pumped from input stream to output stream.</returns>
 33         
public static int PumpStream(Stream input, Stream output, int pumpBufferSize) {
 34             
if (input == null) {
 35                 
throw new ArgumentNullException("input", "Input stream cannot be null.");
 36             
}
 37
 38             
if (output == null) {
 39                 
throw new ArgumentNullException("output", "Output stream cannot be null.");
 40             
}
 41
 42             
if (pumpBufferSize < 1) {
 43                 
throw new ArgumentOutOfRangeException("pumpBufferSize", pumpBufferSize, "PumpBufferSize cannot be less than 1 byte.");
 44             
}
 45
 46             
byte[] bytes = new byte[pumpBufferSize];
 47             
int numBytesRead;
 48             
int totalBytesRead = 0;
 49             
try {
 50                 
while ((numBytesRead = input.Read(bytes, 0, pumpBufferSize)) > 0) {
 51                     
totalBytesRead += numBytesRead;
 52                     
output.Write(bytes, 0, numBytesRead);
 53                 
}
 54                 
output.Flush();
 55             
} finally {
 56                 
input.Close();
 57             
}
 58
 59             
return totalBytesRead;
 60         
}
 61     
}
 62
}