SoftwareGuy74
SoftwareGuy74

Reputation: 63

Possible to write/read from same stream simultaneously?

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.

Upvotes: 5

Views: 6746

Answers (2)

spender
spender

Reputation: 120518

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.

Upvotes: 1

TypeIA
TypeIA

Reputation: 17258

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.

Upvotes: 7

Related Questions