简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM