Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to write/read from same stream simultaneously?

Tags:

c#

.net

I guess I could try this out before asking, but just wanted to be sure it was even possible before I wasted any time.

I'm trying to eliminate the need for an in memory or physical file buffer. I want to download a file via HTTP, or FTP (or anything for that matter) and as the stream is getting filled, I want simultaneously start uploading that stream (via HTTP, FTP, etc) to somewhere else.

Is this even possible in .NET? The only way I can think of doing that "traditionally" is to first allow the stream to finish, and then copy that stream from memory into another stream, but I want to use the least amount of resources (time, memory, etc) as possible.

like image 406
SoftwareGuy74 Avatar asked Feb 14 '23 01:02

SoftwareGuy74


2 Answers

This is entirely possible, no problem; but you wouldn't use the same stream, you would use two different ones, and pipe data from the input to the output in a loop.

like image 70
TypeIA Avatar answered Feb 24 '23 04:02

TypeIA


Should be fine. Recent versions of .Net support Stream.CopyToAsync(otherStream) (or its non-async counterpart), which is pretty much a complete solution for you.

like image 37
spender Avatar answered Feb 24 '23 04:02

spender