Streams
Hello. I am totally new to file management. I was working on my discord bot when I needed to process an image sent by the user and send it back, this introduced me to streams.
So I assume a memory stream is the correct approach here, but I would appreciate an explanation as to why streams exist at all. It seems much more natural to me to just store the data in variables like any other class. Why are things like images so special?
7 Replies
so you can upload and download the image without having to load the entire thing into memory at once
but what does the stream do?
My code:
await response.Content.CopyToAsync(memStream);
Why not something like
byte[] image = await response.Content.GetBytes();
I'm asking because the whole concept of a byte stream is kind of confusingwell in that case it's basically the same, since a memory stream is just a stream wrapper around an array
but like, say you have a huge file, many gigabytes, and you need to process it
like maybe it's a bunch of numbers you need to sum up
I think I still don't understand exactly what a stream does
you could just load the whole file at once, and work on that byte array, but you're taking huge amounts of memory for no reason
(also .NET arrays are limited in size, but that's beside the point)
with a stream you can just pull in chunks at a time to work on
you tell the stream "give me the next X bytes in the file" or "give me the next line" (if you're working with text)
So it spreads out the workload
I see
Thanks
not exactly, but it represents data that isn't necessarily in RAM like a file or network connection
so you can read smaller pieces at a time