简体   繁体   English

FileStream.copyTo(Net.ConnectStream)实习生会发生什么?

[英]FileStream.copyTo(Net.ConnectStream) what happens intern?

this code works fine. 此代码可以正常工作。 My question is what happens within the Net.ConnectionStream when i use the CopyTo() method? 我的问题是,当我使用CopyTo()方法时,Net.ConnectionStream中会发生什么?

System.Net.HttpWebRequest request 
using (FileStream fileStream = new FileStream("C:\\myfile.txt")
{                        
    using (Stream str = request.GetRequestStream())
    {                   
         fileStream.CopyTo(str);
    }
}

More specific: What happens to the data? 更具体地说:数据发生了什么?
1. write into the memory and upload then? 1.写入内存然后上传? (what's with big files?) 2. write into the network directly? (大文件是什么?)2.直接写入网络? (how does that work?) (这是如何运作的?)

Thanks for your answers 谢谢你的回答

It creates a byte[] buffer and calls Read on the source and Write on the destination until the source doesn't have anymore data. 它创建一个byte[]缓冲区,并在源上调用Read并在目标上调用Write ,直到源不再有数据为止。

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. 因此,对大文件执行此操作时,您不必担心内存不足,因为您只会分配与缓冲区大小一样多的内存,默认情况下为81920字节。

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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM