DEV Community

loading...

Answer: Creating a byte array from a stream

boyukbas profile image baris ・1 min read

It really depends on whether or not you can trust s.Length. For many streams, you just don't know how much data there will be. In such cases - and before .NET 4 - I'd use code like this:

public static byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16*1024];

Discussion (0)

pic
Editor guide