如何传递一个OutputStream在C#中的InputStream? [英] How do you pass an OutputStream as an InputStream in C#?
问题描述
我的目标是从一个流中读取,转换该流,并把它作为一个输入库,接受流
阅读。
My goal is to read from one stream, transform that stream, and use it as an input to a library that accepts a Stream
to read.
我使用两个不同的库。一个需要输出流
并将其转换。让我们把它叫做 TransformingOutputStream
。它的用途是:
I am using two different libraries. One takes an output Stream
and transforms it. Let's call it TransformingOutputStream
. Its intended use is:
var outputStream = new TransformingOutputStream(finalDestinationOutputStream);
inputStream.CopyTo(outputStream);
我使用接受输入另一个库流
。它不管它的需求,然后从该流中读取。它的用途是:
I'm using another library that accepts an input Stream
. It does whatever it needs and then reads from that stream. Its intended use is:
MagicStreamReadingLibrary.ProcessStream(someInputStream);
我无法通过 TransformingOutputStream
将其因为它的用途是要的写到的,不是读的。我没有对任何磁带库控制。
I can't pass TransformingOutputStream
to it because its intended use is to be written-to, not read-from. I do not have control over either library.
如何挂钩-了 TransformingOutputStream
来的库函数需要的阅读的从输入流
?
How do I hook-up the TransformingOutputStream
to the library function that requires reading from an input Stream
?
推荐答案
到目前为止,这是最好的工作示例我有,使用匿名管道:
So far this is the best working example I have, using Anonymous Pipes:
using( var pipeServer = new AnonymousPipeServerStream( PipeDirection.Out ) ) {
var pipeServerTask = Task.Run(
async () => {
using( var stream = getInputStream() ) {
await stream.CopyToAsync( new TransformingOutputStream( pipeServer ) );
}
pipeServer.WaitForPipeDrain();
pipeServer.Dispose();
} );
using( var client = new AnonymousPipeClientStream( PipeDirection.In, pipeServer.ClientSafePipeHandle ) ) {
MagicStreamReadingLibrary.ProcessStream( client );
}
pipeServerTask.Wait();
}
这篇关于如何传递一个OutputStream在C#中的InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!