一,使用固定大小的byte[] bytes=new byte[1024],循环读取netStream流,依次存入 Memory Stream mstream=new MemoryStream()
方法如下:
public static byte[] GetBytes(Stream stream)
{
using (MemoryStream mstream = new MemoryStream())
{
byte[] bytes = new byte[1024]; //此处不易设置太大或太小的值,且应该为2的次方
if (stream.CanRead)
{
while (true)
{
int length = stream.Read(bytes, 0, bytes.Length);
if (length <= 0)
{
break;
}
mstream.Write(bytes, 0, length);
}
}
return mstream.ToArray();
}
}