Copies one stream to another. - CSharp System.IO

CSharp examples for System.IO:Stream

Description

Copies one stream to another.

Demo Code


using System.IO;//from  www.  j  av  a2  s  . co  m
using System.Diagnostics;
using System;

public class Main{
        /// <summary>
        /// Copies one stream to another.
        /// </summary>
        /// <param name="input">Input stream.</param>
        /// <param name="output">Output stream.</param>
        /// <param name="size">Optional count of bytes to copy. 0 indicates whole input stream from current should be copied.</param>
        protected static int CopyStream(Stream input, Stream output, int size)
        {
            byte[] bytes = new byte[4096];
            int total = 0;
            int read = 0;
            do
            {
                read = Math.Min(bytes.Length, size - total);
                read = input.Read(bytes, 0, read);
                if (0 == read)
                {
                    break;
                }

                output.Write(bytes, 0, read);
                total += read;
            } while (0 == size || total < size);

            return total;
        }
}

Related Tutorials