Input/Output 10: BufferedStream
Written by:
, 2009-04-16 21:30:48
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;
BufferedStream bufferedStream = new BufferedStream(
openFileDialog.OpenFile());
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.
Source:
http://www.ondotnet.com/pub/a/dotnet/2002/08/05/io.html?page=5