如何在.net中将一个流的内容写入另一个流?

时间:2021-07-11 22:14:45

I often run into the problem that I have one stream full of data and want to write everything of it into another stream.

我经常遇到一个问题,即我有一个数据流,并希望将其中的所有内容写入另一个数据流。

All code-examples out there use a buffer in form of a byte-array.

所有代码示例都使用字节数组形式的缓冲区。

Is there a more elegant way to this?

有更优雅的方式吗?

If not, what's the ideal size of the buffer. Which factors make up this value?

如果不是,那么缓冲区的理想大小是多少。哪些因素构成了这个价值?

6 个解决方案

#1


16  

Regarding the ideal buffer size:

关于理想的缓冲区大小:

"When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes)."

“使用Read方法时,使用与流的内部缓冲区大小相同的缓冲区更为有效,其中内部缓冲区设置为所需的块大小,并始终读取小于块大小。如果在构造流时未指定内部缓冲区的大小,则其默认大小为4千字节(4096字节)。“

Any stream-reading process will use Read(char buffer[], int index, count), which is the method this quote refers to.

任何流读取过程都将使用Read(char buffer [],int index,count),这是此引用所引用的方法。

http://msdn.microsoft.com/en-us/library/9kstw824.aspx (Under "Remarks").

http://msdn.microsoft.com/en-us/library/9kstw824.aspx(在“备注”下)。

#2


68  

In .NET 4.0 we finally got a Stream.CopyTo method! Yay!

在.NET 4.0中,我们终于得到了一个Stream.CopyTo方法!好极了!

#3


7  

I'm not sure if you can directly pipe one stream to another in .NET, but here's a method to do it with an intermediate byte buffer. The size of the buffer is arbitrary. The most efficient size will depend mostly on how much data you're transferring.

我不确定你是否可以在.NET中直接将一个流传输到另一个流,但是这里有一个使用中间字节缓冲区的方法。缓冲区的大小是任意的。最有效的大小主要取决于您传输的数据量。

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

#4


5  

BufferedStream.CopyTo(Stream)

BufferedStream.CopyTo(流)

#5


2  

Read data in FileStream into a generic Stream

将FileStream中的数据读入通用流

will probably have some directions to go in

可能会有一些方向进入

#6


2  

I'm not aware of a more elegant way, than using a buffer.

我不知道比使用缓冲区更优雅的方式。

But the size of a buffer can make a difference. Remember the issues about Vista's File Copy? It's reason was (basically) changing the buffer size. The changes are explained in this blogpost. You can learn the main factors from that post. However, this only applies for file copying. In applications probably you do a lot of memory copies, so in that case, the 4KB could be the best buffer size, as recommended by the .NET documentation.

但是缓冲区的大小可以产生影响。还记得有关Vista文件复制的问题吗?这是(基本上)改变缓冲区大小的原因。这篇博文中解释了这些变化。您可以从该帖子中了解主要因素。但是,这仅适用于文件复制。在应用程序中,您可能会执行大量内存副本,因此在这种情况下,4KB可能是最佳缓冲区大小,如.NET文档所建议的那样。

#1


16  

Regarding the ideal buffer size:

关于理想的缓冲区大小:

"When using the Read method, it is more efficient to use a buffer that is the same size as the internal buffer of the stream, where the internal buffer is set to your desired block size, and to always read less than the block size. If the size of the internal buffer was unspecified when the stream was constructed, its default size is 4 kilobytes (4096 bytes)."

“使用Read方法时,使用与流的内部缓冲区大小相同的缓冲区更为有效,其中内部缓冲区设置为所需的块大小,并始终读取小于块大小。如果在构造流时未指定内部缓冲区的大小,则其默认大小为4千字节(4096字节)。“

Any stream-reading process will use Read(char buffer[], int index, count), which is the method this quote refers to.

任何流读取过程都将使用Read(char buffer [],int index,count),这是此引用所引用的方法。

http://msdn.microsoft.com/en-us/library/9kstw824.aspx (Under "Remarks").

http://msdn.microsoft.com/en-us/library/9kstw824.aspx(在“备注”下)。

#2


68  

In .NET 4.0 we finally got a Stream.CopyTo method! Yay!

在.NET 4.0中,我们终于得到了一个Stream.CopyTo方法!好极了!

#3


7  

I'm not sure if you can directly pipe one stream to another in .NET, but here's a method to do it with an intermediate byte buffer. The size of the buffer is arbitrary. The most efficient size will depend mostly on how much data you're transferring.

我不确定你是否可以在.NET中直接将一个流传输到另一个流,但是这里有一个使用中间字节缓冲区的方法。缓冲区的大小是任意的。最有效的大小主要取决于您传输的数据量。

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

#4


5  

BufferedStream.CopyTo(Stream)

BufferedStream.CopyTo(流)

#5


2  

Read data in FileStream into a generic Stream

将FileStream中的数据读入通用流

will probably have some directions to go in

可能会有一些方向进入

#6


2  

I'm not aware of a more elegant way, than using a buffer.

我不知道比使用缓冲区更优雅的方式。

But the size of a buffer can make a difference. Remember the issues about Vista's File Copy? It's reason was (basically) changing the buffer size. The changes are explained in this blogpost. You can learn the main factors from that post. However, this only applies for file copying. In applications probably you do a lot of memory copies, so in that case, the 4KB could be the best buffer size, as recommended by the .NET documentation.

但是缓冲区的大小可以产生影响。还记得有关Vista文件复制的问题吗?这是(基本上)改变缓冲区大小的原因。这篇博文中解释了这些变化。您可以从该帖子中了解主要因素。但是,这仅适用于文件复制。在应用程序中,您可能会执行大量内存副本,因此在这种情况下,4KB可能是最佳缓冲区大小,如.NET文档所建议的那样。