有关的工作,我的项目的规范使用。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); }更多推荐
从一个流复制到另一个?
发布评论