complete-reference-vb_net_54
complete-reference-vb_net_54
• FileStream Bridges a Stream object to a file for synchronous and asynchronous read and write
operations. This class can be derived from and instantiated.
• MemoryStream Creates a Stream in memory that can read a block of bytes from a current stream
and write the data to a buffer. This class can be derived from and instantiated.
• CrytoStream Defines a Stream that bridges data objects to cryptographic services. This class can be
derived from and instantiated.
• NetworkStream Defines a Stream that bridges data objects to network services. This class can be
derived from and instantiated.
The preceding classes provide data streaming operations. This data may be persisted by bridging certain
objects to various backing stores. However, the Stream objects do not necessarily need to be saved. Your
objects might stream volatile information, resident only in memory. For algorithms not requiring backing
stores and other output or input devices, you can simply use MemoryStream objects.
MemoryStream objects support access to a nonbuffered stream that encapsulates data directly accessible in
memory. The object has no backing store and can thus be used as a temporary buffer. On the other hand, when
you need to write data to the network, you'll use classes like NetworkStream. Your standard text or binary
streams can also be managed using the FileStream class. Stream objects enable you to obtain random access
to files through the use of a Seek method, discussed shortly.
Another Stream derivative you will find yourself using on many occasions is the CryptoStream class. We'll
go over it briefly later in this chapter. This class is also not included in the System.IO namespace but has been
added to the System.Security.Cryptography namespace.
BufferedStream objects provide a buffering bridge for other Stream objects, such as the NetworkStream
object. The BufferedStream object stores the stream data in memory in a special byte cache, which cuts
down on the number of calls the object needs to be made to the OS.
FileStream
FileStream objects can be used to implement all of the standard input, output, and error stream functionality.
With these objects, you can read and write to file objects on the file system. With it you can also bridge to
various file−related operating system handles, such as pipes, standard input, and standard output. The input
and output of data is buffered to facilitate performance.
The File class, discussed earlier in this chapter, is typically used to create and bridge FileStream objects to
files based on file paths and the standard input, standard output, and standard error devices. MemoryStream
similarly bridges to a byte array.
The principal method in a FileStream object is Seek. It supports random access to files and allows the
read/write position to be moved to any position within a file. The location is obtained using byte offset
reference point parameters. The following code demonstrates the creation and opening of a file and the
subsequent bridge to the FileStream object used to write to the file:
In the preceding code, a file is opened, or created if it does not already exist, and information is appended to
the end of the file. The contents of the file are then written to standard output for display.
Byte offsets are relative to a seek reference point. A seek reference point can be the beginning of the file, a
position in the file, or the end of the file. The three SeekOrigin constructs are the properties of the
538