FileStream.copyTo(Net.ConnectStream)发生什么实习? [英] FileStream.copyTo(Net.ConnectStream) what happens intern?

查看:509
本文介绍了FileStream.copyTo(Net.ConnectStream)发生什么实习?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码工作正常。我的问题是,当我使用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)发生什么实习?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆