从一个流复制到另一个? [英] Copying from one stream to another?

查看:153
本文介绍了从一个流复制到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有关的工作,我的项目的规范使用。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);
}

这篇关于从一个流复制到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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