如何将写入流1的内容传输到流2中? [英] How to pipe what is written to Stream 1 into Stream 2?
问题描述
这是我的情况:
producer.WriteStream(stream);
consumer.ReadStream(stream);
我想要一些东西,它允许将 producer
生成的字节逐步转移到 consumer
.
I want something that allows the bytes generated by the producer
to be progressively transfered to the consumer
.
我可以将所有内容写入 MemoryStream
,然后倒带并在 consumer
上读取,但这会导致大量的内存消耗.
I could write everything to a MemoryStream
, then rewind it and read it on the consumer
, but that is causing huge memory consumption.
我该如何实现?
推荐答案
使用管道作为数据的基础传输,您可以拥有写入流"(服务器)和读取流"(客户端),以实现以下目的:这样的沟通机制.
Using pipes as the underlying transport for your data, you can have a "write stream" (server) and a "read stream" (client) that allow for such communication mechanism.
使用匿名管道或命名管道(如果需要进程间通信),都非常简单.要创建管道流,请执行以下操作:
It quite simple to do, either with anonymous pipes or named pipes (if you need inter-proc communication). To create your the pipe streams:
AnonymousPipeServerStream pipeServer = new AnonymousPipeServerStream();
AnonymousPipeClientStream pipeClient =
new AnonymousPipeClientStream(pipeServer.GetClientHandleAsString());
现在您可以将其用作&阅读:
Now you can use these as to write & read:
producer.WriteStream(pipeServer);
// somewhere else...
consumer.ReadStream(pipeClient);
这篇关于如何将写入流1的内容传输到流2中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!