MorkaLork Development

Interesting stuff I've picked up over the years...

Input/Output 10: BufferedStream

2009-04-16 21:30:48 | 437 views | csharp class buffered stream io

The Basics

The BufferedStream class is used to wrap a Stream object and supports buffering to improve performance. There are two constructors that you can use to create a BufferedStream object. The first one accepts a Stream object. The BufferedStream object constructed using this constructor has a buffer size of 4,096 bytes. The second constructor accepts a Stream object as well as an Integer specifying the size of the buffer, allowing you to set your own buffer size.

As an example of how to use the BufferedStream, consider the following code, which wraps the Stream object returned from an OpenFileDialog in a BufferedStream object and reads the first 128 bytes of the selected file:

OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
string filepath = openFileDialog.FileName;
// Open the file using the OpenFile method
BufferedStream bufferedStream = new BufferedStream(
Byte[] bytes = new Byte[128];
bufferedStream.Read(bytes, 0, 128);

Note that the BufferedStream class is derived from the Stream class; therefore, it inherits all of the Stream class' methods and properties.


Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.