static byte[] GetBytes(Stream stream)
{
byte[] bytes;
bool resize = false;
if (stream.CanSeek)
bytes = new byte[stream.Length];
else
{
resize = true;
bytes = new byte[4096*2];
}
int offset = 0;
while (true)
{ int read = stream.Read(bytes, offset, bytes.Length-offset);
if (read == 0)
break;
if ((offset+=read) == bytes.Length && resize)
{
byte[] newBytes = new byte[bytes.Length+4096*2];
Buffer.BlockCopy(bytes, 0, newBytes, 0, offset);
bytes = newBytes;
}
}
if (resize)
{
byte[] newBytes = new byte[offset];
Buffer.BlockCopy(bytes, 0, newBytes, 0, offset);
bytes = newBytes;
}
return bytes;
}