是否有可能克隆.NET流? [英] Is it Possible to Clone a .NET Stream?
问题描述
我们可以克隆一个流?
推荐答案
没有,流通常是指某种(套接字,文件句柄等)的本地资源,所以他们不能被克隆和序列化。此外许多流是正向的,并且不支持查找,所以你可能甚至无法从流重新读取。
No, streams usually refer to local resources of some kind (a socket, a file handle, etc) and so they can't be cloned or serialized. Furthermore many streams are forward-only and do not support seeking so you might not even be able to re-read from a stream.
您可以从一个可读的流做的虽然是将其复制到MemoryStream它可以移动作为字节数组。
What you can do from a readable stream though is copy it into a MemoryStream which can be moved around as a byte array.
请参阅以下职位为code片断展示了如何做到这一点: <一href="http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c">http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c
See the following post for a code snippet showing how to do this: http://stackoverflow.com/questions/230128/best-way-to-copy-between-two-stream-instances-c
public static void CopyStream(Stream input, Stream output)
{
byte[] buffer = new byte[32768];
while (true)
{
int read = input.Read (buffer, 0, buffer.Length);
if (read <= 0)
return;
output.Write (buffer, 0, read);
}
}
这篇关于是否有可能克隆.NET流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!