The foundation for managed I/O in the .NET Framework is the Stream class. The Stream class is an abstract class that must be inherited to create either a base stream or a composable stream. The Stream class provides simple common methods to read and write data directly to a store, such as base streams, or to another stream, such as composable streams.
Once you have successfully created a stream, you can begin performing I/O operations. Table 2-1 describes the main operations that are generally available in all stream classes to handle I/O. Depending on the stream at hand, it will not be possible to implement all operations for various technical reasons. If a stream does not handle a particular method, generally it will throw a NotSupportedException from the System namespace.
Method | Description |
---|---|
BeginRead | Allows data to be read asynchronously from a data store |
BeginWrite | Allows data to be written asynchronously to a data store |
Close | Closes a stream for further I/O operations and releases any operating system resources associated with a stream |
EndRead | Completes asynchronous read operations started from BeginRead |
EndWrite | Completes asynchronous write operations started from BeginWrite |
Flush | Forces any buffered data associated with a stream to be written to the backing store |
Read | Allows one or more data bytes to be read from a backing store |
ReadByte | Allows one byte to be read from a backing store |
Seek | Allows the Position property of the stream to be set |
SetLength | Allows the size of the backing store in bytes to be controlled |
Write | Allows bytes to be written to a backing store |
WriteByte | Allows one byte to be written to a backing store |
Stream classes also have several properties that help streams work with a backing store. Table 2-2 describes the available properties.
Property | Description |
---|---|
CanRead | Determines if the stream can be read |
CanSeek | Determines if the stream allows you to change the Position property |
CanWrite | Determines if the stream can be written to |
Length | Reports the size in bytes of the backing store |
Position | Controls the location in a stream where the next byte will be read or written to a backing store |
Reading and writing data to stream classes is handled using byte-type arrays. This method is fine if you re developing an application that simply deals with data in binary form. Later in this chapter, we ll present stream reader and writer classes that allow you to read and write text data or other binary data types. For our discussions of the stream, we ll stick to reading and writing data in byte-type form.
Our discussion of performing I/O with streams in this chapter centers on handling synchronous I/O patterns where the Read or Write stream methods will block until the I/O operation is completed with the system resource. Depending on the resource, blocking can be very limiting to your application, especially if you need to service something else, such as the user interface of your application, and are stuck waiting all day to read data from a system resource such as a network. The .NET Framework also allows you to perform I/O asynchronously. The next chapter will describe threading and the asynchronous I/O pattern, where you can use the BeginRead , EndRead , BeginWrite , and EndWrite methods to avoid blocking on I/O operations. When performing I/O on streams, you should choose between either synchronous or asynchronous I/O patterns and never mix the two styles.