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

  • Hands On: New VS Code Insiders Build Creates Web Page from Image in Seconds

    New Vision support with GitHub Copilot in the latest Visual Studio Code Insiders build takes a user-supplied mockup image and creates a web page from it in seconds, handling all the HTML and CSS.

  • Naive Bayes Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the naive Bayes regression technique, where the goal is to predict a single numeric value. Compared to other machine learning regression techniques, naive Bayes regression is usually less accurate, but is simple, easy to implement and customize, works on both large and small datasets, is highly interpretable, and doesn't require tuning any hyperparameters.

  • VS Code Copilot Previews New GPT-4o AI Code Completion Model

    The 4o upgrade includes additional training on more than 275,000 high-quality public repositories in over 30 popular programming languages, said Microsoft-owned GitHub, which created the original "AI pair programmer" years ago.

  • Microsoft's Rust Embrace Continues with Azure SDK Beta

    "Rust's strong type system and ownership model help prevent common programming errors such as null pointer dereferencing and buffer overflows, leading to more secure and stable code."

  • Xcode IDE from Microsoft Archrival Apple Gets Copilot AI

    Just after expanding the reach of its Copilot AI coding assistant to the open-source Eclipse IDE, Microsoft showcased how it's going even further, providing details about a preview version for the Xcode IDE from archrival Apple.

Subscribe on YouTube

Upcoming Training Events