Given two external APIs with one offering a download API and the other offering an upload API, both only allow using a Stream
for data exchange, I need a way to efficiently transfer the data directly from download straight to upload.
Assume the APIs are as simple as:
Download (string url, Stream stream)
Upload (string url, Stream source)
I can't find a Stream
implementation in the .NET framework that offers this capability.
This is not a case for Stream.CopyTo
as both APIs implement their own read and write IO loops. If either of the APIs had returned a custom Stream
implementation then Stream.CopyTo
would be the answer, but since they both accept an input Stream
for reading or writing, one has to implement a custom Stream
that acts as both a sink (for the downloader) and a source (for the uploader) and puts back-pressure on the slower of the two to keep memory buffers from growing out of control.