How to copy one Stream to a byte array with the smallest C# code?

Posted by estourodepilha.com on Stack Overflow See other posts from Stack Overflow or by estourodepilha.com
Published on 2009-06-04T13:17:36Z Indexed on 2010/03/31 16:43 UTC
Read the original article Hit count: 153

Filed under:
|
|

Until now I am counting 12 LoCs. Could you make it smaller?

using (Stream fileStream = File.OpenRead(fileName))
{
    using (BinaryReader binaryReader = new BinaryReader(fileStream))
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            byte[] buffer = new byte[256];
            int count;
            int totalBytes = 0;
            while ((count = binaryReader.Read(buffer, 0, 256)) > 0)
            {
                memoryStream.Write(buffer, 0, count);
                totalBytes += count;
            }
            memoryStream.Position = 0;
            byte[] transparentPng = new byte[totalBytes];
            memoryStream.Read(transparentPng, 0, totalBytes);
        }
    }
}

© Stack Overflow or respective owner

Related posts about .NET

Related posts about c#