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

编程入门 行业动态 更新时间:2024-10-26 19:35:26
本文介绍了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)发生什么实习?

本文发布于:2023-11-13 09:22:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583995.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   copyTo   FileStream   ConnectStream   Net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!