Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between BufferedStream and MemoryStream in terms of application?

Tags:

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?

like image 711
softwarematter Avatar asked Sep 17 '09 16:09

softwarematter


People also ask

What is MemoryStream used for?

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.

What is the difference between MemoryStream and FileStream?

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.

What's the difference between streaming and buffering?

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.

What is the relationship between a stream pointer and buffer area?

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.


1 Answers

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.

like image 87
Jon Skeet Avatar answered Sep 17 '22 16:09

Jon Skeet