Source

main / Source / WebService / Web.Common / Extensions / StreamExtensions.cs

Full commit
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace DataUp.Web.Common
{
    public static class StreamExtensions
    {
        public static void CopyToStream(this Stream source, Stream target)
        {
            byte[] copyBuf = new byte[0x1000];
            int bytesRead = 0;
            int bufSize = copyBuf.Length;

            source.Position = 0;

            while ((bytesRead = source.Read(copyBuf, 0, bufSize)) > 0)
                target.Write(copyBuf, 0, bytesRead);
        }

        public static void CopyTo(this Stream source, string filePath)
        {
            using (FileStream writeStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
                source.CopyTo(writeStream);
        }

        public static byte[] GetBytes(this Stream source)
        {
            byte[] ImageData = null;

            if (source != null)
            {
                // Create a byte array of file stream length
                ImageData = new byte[source.Length];

                source.Position = 0;

                //Read block of bytes from stream into the byte array
                source.Read(ImageData, 0, System.Convert.ToInt32(source.Length));
            }

            return ImageData;
        }
    }
}