views:

1879

answers:

5

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?

+2  A: 

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).

Anton Gogolev
Do you have a definitive list that shows which streams are buffered and which aren't? I hit this question, specifically interesting in `NetworkStream`. I'd like to lose some buffering code as I'm pretty sure it's no needed. Will keep browsing!
Drew Noakes
Turns out, for the `NetworkStream` case, that you can only read and write using `byte[]`, so the buffering is controlled by the caller. `NetworkStream` is also not seekable, so no internal buffer is required.
Drew Noakes
+2  A: 

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.

GvS
+1  A: 

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.

hacker
I can't understand applying a buffer to a `MemoryStream`, as it is already essentially one giant `byte[]` buffer in memory. The sentence "The performance noticeably improved by applying a BufferedStream to existing streams, such as a FileStream or MemoryStream" seems incomplete, and perhaps is missing the negating words "is not". Wondering whether this is in fact a misleading answer...
Drew Noakes
+1  A: 

Either you guys cannot read or cannot understand what you read. It says there is no point in double streaming, why to include a second buffer in FileStream? It is meant to be used by custom streams that DO NOT include a buffer!

Can Bilgin
+3  A: 

According to Brad Abrams, almost never: link

dewald