Thursday 26 April 2012

copy between two Stream instances - C#


public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write (buffer, 0, read);
    }
}
N.B. From .NET 4.0, there's a Stream.CopyTo method:
input.CopyTo(output);
And you have to set the position to 0 afterwards.
input.Position = output.Position = 0;

No comments:

Post a Comment