我有以下方法将字节从套接字流复制到磁盘:
public static void CopyStream(Stream input, Stream output)
{
// Insert null checking here for production
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, bytesRead);
}
}
我比较好奇的是:会buffer
分配在栈上还是堆上?可以肯定的是,我可以使此方法不安全,并将fixed
关键字添加到变量声明中,但如果我不必这样做,我不想这样做。