What is the difference between BufferedStream and MemoryStream in terms of application? Since MemoryStream can be flushed into a file at any time, couldn't it replace BufferedStream?
MemoryStream encapsulates data stored as an unsigned byte array. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application. The current position of a stream is the position at which the next read or write operation takes place.
As the name suggests, a FileStream reads and writes to a file whereas a MemoryStream reads and writes to the memory. So it relates to where the stream is stored.
Buffering is the practice of pre-loading segments of data when streaming video content. Streaming — the continuous transmission of audio or video files from a server to a client — is the process that makes watching videos online possible.
When the buffer is full, consumers of that stream are notified and can consume data from the buffer until is depleted. Then wait for the buffer to be filled again before using that data. It is a place to store something temporarily, in order to mitigate differences between the input speed and output speed.
BufferedStream
is just a buffer over an existing stream. MemoryStream
is a buffer for the whole stream - it isn't chained to another one. You can ask it to write itself to another stream at any time, but that's not the same thing.
One of the principle reasons for buffering is to avoid frequent writes to expensive resources. However, that doesn't mean you want to buffer all the data in memory - just enough to avoid very small writes. For example, if FileStream
didn't have its own buffering strategy, then wrapping it in BufferedStream
could end up with a buffer of only 8K even if you write megabytes of data. As pointed out in the comments though, FileStream
has enough buffering that using BufferedStream
in conjunction with it is pointless.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With