这段代码工作正常。我的问题是,当我使用CopyTo()方法时,Net.ConnectionStream中会发生什么?
this code works fine. My question is what happens within the Net.ConnectionStream when i use the CopyTo() method?
System.Net.HttpWebRequest request using (FileStream fileStream = new FileStream("C:\\myfile.txt") { using (Stream str = request.GetRequestStream()) { fileStream.CopyTo(str); } }:数据会发生什么? 1.写入内存并上传?(大文件是什么?) 2.直接写入网络(如何工作?)
More specific: What happens to the data? 1. write into the memory and upload then? (what's with big files?) 2. write into the network directly? (how does that work?)
感谢您的回答
推荐答案$ c> byte [] 缓冲区并调用在源上读取目的地,直到源没有任何数据。
It creates a byte[] buffer and calls Read on the source and Write on the destination until the source doesn't have anymore data.
所以,当这样做与大文件,你不需要担心内存不足,因为你会默认情况下只分配81920字节。
So when doing this with big files you don't need to be concerned about running out of memory because you'll only allocate as much as the buffer size, 81920 bytes by default.
这是实际的实现 -
Here's the actual implementation -
public void CopyTo(Stream destination) { // ... a bunch of argument validation stuff (omitted) this.InternalCopyTo(destination, 81920); } private void InternalCopyTo(Stream destination, int bufferSize) { byte[] array = new byte[bufferSize]; int count; while ((count = this.Read(array, 0, array.Length)) != 0) { destination.Write(array, 0, count); } }更多推荐
FileStream.copyTo(Net.ConnectStream)发生什么实习?
发布评论