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) 
st.Write(text,0,text.Length)
...more code to work with the file...
st.Close

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)
st.Write(text,0,text.Length)

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)
st.Write(text,0,text.Length)

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 http://blog.learningtree.com/tag/ui/.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube