124 Part II Message Endpoints
value in scenarios involving reporting, document versioning, and other tasks.
For example, if you want to save multiple versions of a contract, you can easily
accomplish this by using files. In addition, loading a file into memory as an
array can be an extremely fast method of accessing information. Files are also
useful for storing flat data when no hierarchical relationships exist.
As with many other technologies, file endpoints can be abused. For example,
if you create extremely large files or files that must be related to other files to
make sense, you will certainly cause problems further down the road. Managed
properly, however, files can provide a simple-to-use, extremely fast method of
storing information.
Files and the File System in .NET
Much of this chapter will focus on handling and manipulating files and the file
system in .NET. This section will provide a brief introduction to using files and
the file system within the Microsoft .NET Framework.
The core building block of file I/O in .NET is the System.IO.Stream
class. This is an abstract class inherited by the following classes in the .NET
Framework:
System.IO.FileStream
System.IO.MemoryStream
System.Security.Cryptography.CryptoStream
System.NET.Sockets.NetworkStream
System.IO.BufferedStream
A stream is a flow of bytes to and from a data store such as a file, memory
address, and so on. A FileStream is used for accessing files randomly or sequen-
tially and storing them in a byte array. The following function can convert a
string to a byte array for use with a FileStream object:
Private Function StringToBytes(ByVal aString As String) As Byte()
Dim aBuffer(aString.Length - 1) As Byte
Dim aEncoder As New System.Text.ASCIIEncoding()
aEncoder.GetBytes(aString, 0, aString.Length, aBuffer, 0)
Return aBuffer
End Function
Although you can convert types to and from other types, as just shown,
to use the FileStream you will need to write a function to convert each type
to and from a byte array. Fortunately, the .NET Framework provides the
System.IO.StreamReader, System.IO.StreamWriter, System.IO.BinaryReader, and

Get Enterprise Integration Solutions now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.