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

  • Death of the Dev Machine?

    Here's a takeaway from this week's Ignite 2020 event: An advanced Azure cloud portends the death of the traditional, high-powered dev machine packed with computing, memory and storage components.

  • COVID-19 Is Ignite 2020's Elephant in the Room: 'Frankly, It Sucks'

    As in all things of our new reality, there was no escaping the drastic changes in routine caused by the COVID-19 pandemic during Microsoft's big Ignite 2020 developer/IT pro conference, this week shifted to an online-only event after drawing tens of thousands of in-person attendees in years past.

  • Visual Studio 2019 v16.8 Preview Update Adds Codespaces

    To coincide with the Microsoft Ignite 2020 IT pro/developer event, the Visual Studio dev team shipped a new update, Visual Studio 2019 v16.8 Preview 3.1, with the main attraction being support for cloud-hosted Codespaces, now in a limited beta.

  • Speed Lines Graphic

    New for Blazor: Azure Static Web Apps Support

    With Blazor taking the .NET web development world by storm, one of the first announcements during Microsoft's Ignite 2020 developer/IT event was its new support in Azure Static Web Apps.

  • Entity Framework Core 5 RC1 Is Feature Complete, Ready for Production

    The first release candidate for Entity Framework 5 -- Microsoft's object-database mapper for .NET -- has shipped with a go live license, ready for production.

Upcoming Events