Practical .NET

Stack Up the Stream Objects to Combine Functionality

Using the Stream objects correctly can speed up your I/O, protect your files and simplify your code.

With the Stream objects, it's not about the individual objects -- it's all about how you put them together. In fact, the key to understanding the Stream objects is realizing how you can chain them together to address complex processing requirements.

For example, the following code creates a FileStream object that adds text to a file called MyFile.text. However, you'll notice that I've declared the variable as a Stream object so that I can point that variable at any object that inherits from the base Stream class:

Dim st As Stream
Dim text() As Byte
st = New FileStream("MyFile.txt",FileMode.Append) 
...more code to work with the file...

From an efficiency point of view, this isn't great code: It's going to access the hard disk on every write. Because reading or writing your hard disk is one of the two slowest things in data processing, I'd prefer to avoid that. So I create a BufferedStream object and pass it my FileStream object. Because my variable is now pointing at the BufferedStream object and the BufferedStream object supports all the methods and properties of the FileStream object, the rest of my code doesn't need to change:

st = New FileStream("MyFile.txt",FileMode.Append) 
st = New BufferedStream(st)

Now, with the BufferedStream object wrapping my code, I won't access my hard disk until I call the Close method.

Similarly, to prevent people reading my file in NotePad, I could pass my BufferedStream object to a new CryptoStream object. This will cause my text to be encrypted on its way to the hard disk (again, without having to change any of the following code because the CryptoStream object also supports all the methods and properties of the FileStream object):

st = New CryptoStream(st,encrypter)

If I was willing to change my code, I might decide that I didn't like working with Byte arrays (as the Stream objects I've been using require). In that case, I could create a StreamWriter and pass my Stream to that, like this:

Dim stw As StreamWriter
stw = New StreamWriter(st)
stw.WriteLine("Hello, world")

That change would let me rewrite my code to work with a WriteLine method that does accept text.

About the Author

Peter Vogel is a system architect and principal in PH&V Information Services. PH&V provides full-stack consulting from UX design through object modeling to database design. Peter tweets about his VSM columns with the hashtag #vogelarticles. His blog posts on user experience design can be found at

comments powered by Disqus


  • .NET Core Ranks High Among Frameworks in New Dev Survey

    .NET Core placed high in a web-dominated ranking of development frameworks published by CodinGame, which provides a tech hiring platform.

  • Here's a One-Stop Shop for .NET 5 Improvements

    Culled from reams of Microsoft documentation, here's a high-level summary of what's new for performance, networking, diagnostics and more, along with links to the nitty-gritty details for those wanting to dig in more.

  • Azure SQL Database Ranked Among Top 3 Databases of 2020

    Microsoft touted the inclusion of Azure SQL Database among the top three databases of 2020 in a popularity ranking by DB-Engines, which collects and manages information about database management systems, updating its lists monthly.

  • Time Tracker Says VS Code Is No. 1 Editor for Devs, Some Working 15+ Hours Per Day

    WakaTime, which does time tracking for programmers, released data for 2020 showing that Visual Studio Code is by far the top editor/IDE used by its coders, some of whom are hacking away for more than 15 hours per day.

Upcoming Events