将一个流的内容复制到另一个流的最佳方法是什么?有标准的实用方法吗?


当前回答

由于没有一个答案涉及从一个流复制到另一个流的异步方式,这里是我在端口转发应用程序中成功使用的一种模式,它将数据从一个网络流复制到另一个网络流。它缺乏异常处理来强调模式。

const int BUFFER_SIZE = 4096;

static byte[] bufferForRead = new byte[BUFFER_SIZE];
static byte[] bufferForWrite = new byte[BUFFER_SIZE];

static Stream sourceStream = new MemoryStream();
static Stream destinationStream = new MemoryStream();

static void Main(string[] args)
{
    // Initial read from source stream
    sourceStream.BeginRead(bufferForRead, 0, BUFFER_SIZE, BeginReadCallback, null);
}

private static void BeginReadCallback(IAsyncResult asyncRes)
{
    // Finish reading from source stream
    int bytesRead = sourceStream.EndRead(asyncRes);
    // Make a copy of the buffer as we'll start another read immediately
    Array.Copy(bufferForRead, 0, bufferForWrite, 0, bytesRead);
    // Write copied buffer to destination stream
    destinationStream.BeginWrite(bufferForWrite, 0, bytesRead, BeginWriteCallback, null);
    // Start the next read (looks like async recursion I guess)
    sourceStream.BeginRead(bufferForRead, 0, BUFFER_SIZE, BeginReadCallback, null);
}

private static void BeginWriteCallback(IAsyncResult asyncRes)
{
    // Finish writing to destination stream
    destinationStream.EndWrite(asyncRes);
}

其他回答

从。net 4.5开始,就有了流。CopyToAsync方法

input.CopyToAsync(output);

这将返回一个Task,完成后可以继续执行,如下所示:

await input.CopyToAsync(output)

// Code from here on will be run in a continuation.

注意,根据对CopyToAsync调用的位置,后面的代码可能继续也可能不继续在调用它的同一个线程上。

调用await时捕获的SynchronizationContext将决定在哪个线程上执行延续。

此外,这个调用(这是一个可能会改变的实现细节)仍然对读写进行排序(它只是没有在I/O完成时浪费线程阻塞)。

从。net 4.0开始,就有了流。CopyTo方法

input.CopyTo(output);

适用于。net 3.5及之前版本

框架中没有任何东西可以帮助实现这一点;你必须手动复制内容,就像这样:

public static void CopyStream(Stream input, Stream output)
{
    byte[] buffer = new byte[32768];
    int read;
    while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write (buffer, 0, read);
    }
}

注1:这个方法将允许你报告进度(x字节读取到目前为止…) 注2:为什么使用固定的缓冲区大小而不是input.Length?因为这个长度可能是不可用的!从文档中可以看出:

如果从Stream派生的类不支持查找,则调用Length、SetLength、Position和Seek会抛出NotSupportedException异常。

MemoryStream有.WriteTo(outstream);

. net 4.0在普通流对象上有. copyto。

net 4.0:

instream.CopyTo(outstream);

简单安全-从原始来源制作新流:

    MemoryStream source = new MemoryStream(byteArray);
    MemoryStream copy = new MemoryStream(byteArray);

下面的代码可以解决这个问题,使用CopyTo将流复制到MemoryStream

Stream stream = new MemoryStream();

//任何需要输入流的函数。在我的情况下,保存PDF文件为流 document.Save(流);

MemoryStream newMs = (MemoryStream)stream;

byte[] getByte = newMs.ToArray();

//注意-请在finally块中处理流,而不是在using块中处理,因为它会抛出一个错误'访问被拒绝,因为流被关闭'

区分“CopyStream”实现的基本问题是:

读取缓冲区的大小 写入的大小 我们是否可以使用多个线程(在读取时写入)。

这些问题的答案导致了CopyStream的巨大不同的实现,这取决于您拥有的流的类型和您试图优化的内容。“最佳”实现甚至需要知道流正在读取和写入的具体硬件。