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

  • Microsoft Revamps Fledgling AutoGen Framework for Agentic AI

    Only at v0.4, Microsoft's AutoGen framework for agentic AI -- the hottest new trend in AI development -- has already undergone a complete revamp, going to an asynchronous, event-driven architecture.

  • IDE Irony: Coding Errors Cause 'Critical' Vulnerability in Visual Studio

    In a larger-than-normal Patch Tuesday, Microsoft warned of a "critical" vulnerability in Visual Studio that should be fixed immediately if automatic patching isn't enabled, ironically caused by coding errors.

  • Building Blazor Applications

    A trio of Blazor experts will conduct a full-day workshop for devs to learn everything about the tech a a March developer conference in Las Vegas keynoted by Microsoft execs and featuring many Microsoft devs.

  • Gradient Boosting Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the gradient boosting regression technique, where the goal is to predict a single numeric value. Compared to existing library implementations of gradient boosting regression, a from-scratch implementation allows much easier customization and integration with other .NET systems.

  • Microsoft Execs to Tackle AI and Cloud in Dev Conference Keynotes

    AI unsurprisingly is all over keynotes that Microsoft execs will helm to kick off the Visual Studio Live! developer conference in Las Vegas, March 10-14, which the company described as "a must-attend event."

Subscribe on YouTube