Mega Code Archive

 
Categories / C# / File Stream
 

Read Stream to End

using System; using System.IO; public static class StreamExtension {     private const Int32 BlockSize = 1024;     public static Byte[] ReadToEnd(this Stream source)     {         MemoryStream buffer = new MemoryStream();         Byte[] block = new Byte[StreamExtension.BlockSize];         Int32 len = source.Read(block, 0, StreamExtension.BlockSize);         while (len > 0)         {             buffer.Write(block, 0, len);             len = source.Read(block, 0, StreamExtension.BlockSize);         }         Byte[] result = new Byte[buffer.Position];         Array.Copy(buffer.GetBuffer(), result, result.Length);         return result;     } }