public static byte[] ReadStream(Stream @is, int initialBufferCapacity)
{
if (initialBufferCapacity <= 0)
{
throw new ArgumentException("Bad initialBufferCapacity: " + initialBufferCapacity);
}
byte[] buffer = new byte[initialBufferCapacity];
int cursor = 0;
for (; ; )
{
int n = @is.Read(buffer, cursor, buffer.Length - cursor);
if (n < 0)
{
break;
}
cursor += n;
if (cursor == buffer.Length)
{
byte[] tmp = new byte[buffer.Length * 2];
System.Array.Copy(buffer, 0, tmp, 0, cursor);
buffer = tmp;
}
}
if (cursor != buffer.Length)
{
byte[] tmp = new byte[cursor];
System.Array.Copy(buffer, 0, tmp, 0, cursor);
buffer = tmp;
}
return buffer;
}