Question

The MSDN site states:

A buffer is a block of bytes in memory used to cache data, thereby reducing the number of calls to the operating system. Buffers improve read and write performance. A buffer can be used for either reading or writing, but never both simultaneously. The Read and Write methods of BufferedStream automatically maintain the buffer.

Should I use this class in every possible occasion?

Was it helpful?

Solution

The following is some text from an online course I am taking:

The BufferedStream class is a concrete class that extends the Stream class and is used to provide an additional memory buffer to another type of stream, both synchronously and asynchronously. The BufferedStream class must be configured to either read or write when an instance of the class is created, but the BufferedStream cannot be configured to perform both the tasks at the same time.

Microsoft improved the performance of all streams in the .NET Framework by including a built-in buffer. The performance noticeably improved by applying a BufferedStream to existing streams, such as a FileStream or MemoryStream. Applying a BufferedStream to an existing .NET Framework stream results in a double buffer.

The most common application of the BufferedStream class is in custom stream classes that do not include a built-in buffer.

OTHER TIPS

According to Brad Abrams, almost never: link

No, there is zero benefit from wrapping a BufferedStream around a FileStream. We copied BufferedStream’s buffering logic into FileStream about 4 years ago to encourage better default performance... In fact, I don’t think there are any Streams in the .NET Framework that require it, but it might be needed by custom Stream implementations if they do not do buffering by default.

Best case I know of is when BinaryFormatter serialize/deserialize directly from NetworkStream. Use of BufferedStream inbetween increase performance tenfold.

What must be used in every possible occasion is common sense. There's no use in utilizing this class when reading-writing to-from a MemoryStream, but it might be quite useful when doing network or disk IO (if Streams for these subsystems do not do buffering on their own).

The normal file I/O streams are already buffered by using a StreamReader/StreamWriter.

Since read/write operations on streams, normally use the Read/Write methods that take a byte array, you will naturally provide some buffering yourself.

If you use very small arrays, or use WriteByte, you might get better performance by using a BufferedStream in between.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top