从一个流复制到另一个?

编程入门 行业动态 更新时间:2024-10-25 14:30:10
本文介绍了从一个流复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有关的工作,我的项目的规范使用。NET 2.0,所以我不明白带来后来就得心应手 CopyTo从功能。

For work, the specification on my project is to use .Net 2.0 so I don't get the handy CopyTo function brought about later on.

我要响应流从 HttpWebResponse 复制到另一个流(最有可能是的MemoryStream ,但它可能是流)的任何子类。我的正常的战术一直沿着线的东西:

I need to copy the response stream from an HttpWebResponse to another stream (most likely a MemoryStream, but it could be any subclass of Stream). My normal tactic has been something along the lines of:

BufferedStream bufferedresponse = new BufferedStream(HttpResponse.GetResponseStream()); int count = 0; byte[] buffer = new byte[1024]; do { count = bufferedresponse.Read(buffer, 0, buffer.Length); target.Write(buffer, 0, count); } while (count > 0); bufferedresponse.Close();

有没有更有效的方法来做到这一点?是否缓冲区的大小真的重要吗?这是从一个流至另一个的.Net 2.0?

Are there more efficient ways to do this? Does the size of the buffer really matter? What is the best way to copy from one stream to another in .Net 2.0?

P.S复制的最佳方式。这是下载大,200 + MB GIS TIF图像。当然,可靠性是至关重要的。

P.S. This is for downloading large, 200+ MB GIS tif images. Of course reliability is paramount.

推荐答案

这是一个方便的功能。是的,缓冲区的大小事宜。增加它可能给你的大文件更好的性能。

This is a handy function. And yes, the buffer size matters. Increasing it might give you better performance on large files.

public static void WriteTo(Stream sourceStream, Stream targetStream) { byte[] buffer = new byte[0x10000]; int n; while ((n = sourceStream.Read(buffer, 0, buffer.Length)) != 0) targetStream.Write(buffer, 0, n); }

更多推荐

从一个流复制到另一个?

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

发布评论

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

>www.elefans.com

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