public static void CopyTo(this Stream input, Stream output)
{
// assert these are the right kind of streams
if (input == null)
{
throw new ArgumentNullException("input", "Input stream was null");
}
if (output == null)
{
throw new ArgumentNullException("output", "Output stream was null");
}
if (!input.CanRead)
{
throw new ArgumentException("Input stream must support CanRead");
}
if (!output.CanWrite)
{
throw new ArgumentException("Output stream must support CanWrite");
}
// skip if the input stream is empty (if seeking is supported)
if (input.CanSeek)
{
if (input.Length == 0)
{
return;
}
}
// copy it
const int Size = 4096;
byte[] bytes = new byte[Size];
int numBytes;
while ((numBytes = input.Read(bytes, 0, Size)) > 0)
{
output.Write(bytes, 0, numBytes);
}
}