Inside C#, Second Edition: File I/O with Streams – Part 1

Environment: .NET, C#

Page
 1 
 2 
 3 
 4 
 5 
 6 

From:
Inside Visual C#, Second Edition
By Tom Archer
ISBN: 0735616485
Published by Microsoft Press

Introduction

This chapter divides neatly into two main topics.
First, we.ll consider the classes provided by the .NET Framework classes,
which meet the lower-level data transfer requirements of the streams-based
I/O framework. These classes further divide into stream classes and file system classes.that is, classes that actually represent data streams, and classes that represent file system objects such as files and directories. Then we.ll look at how you can enhance any custom class to allow it
to fit seamlessly into the standard I/O framework. This enhancement is based on a standard attribute that marks your class as capable of being serialized. The serialization process is used in conjunction with the streams classes to stream your custom class objects from one place to another.in memory, to a remote location, or to persistent storage.
As part of our exploration of the streams framework, we.ll consider the different types of stream, types of file system objects, and potential application environments, including Microsoft Windows.based and Web-based environments.

Stream Classes

The .NET Framework classes offer a streams-based I/O framework, with the core classes in the System.IO namespace. All classes that represent streams inherit from the Stream class, and the key classes are listed in Table 1.


Table 1 – String and WriteLine Format Specifiers

























































Class



Description



Stream



The abstract base class Stream supports reading and writing bytes.



FileStream



In addition to basic Stream behavior, this class supports random access to files through its Seek method and supports both synchronous and asynchronous operation.



MemoryStream



A nonbuffered stream whose encapsulated data is directly accessible in memory. This stream has no backing store and might be useful as a temporary buffer.



BufferedStream



A Stream that adds buffering to another Stream, such as a NetworkStream. (FileStream already has buffering internally, and a MemoryStream doesn’t need buffering.) A BufferedStream object can be composed around some types of streams to improve read and write performance.



TextReader



The abstract base class for StreamReader and StringReader objects. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextReader are designed for Unicode character output.



StreamReader



Reads characters from a Stream, using Encoding to convert characters to and from bytes.



StringReader



Reads characters from a String. StringReader allows you to treat a String with the same API; thus, your output can be either a Stream in any encoding or a String.



TextWriter



The abstract base class for StreamWriter and StringWriter objects. While the implementations of the abstract Stream class are designed for byte input and output, the implementations of TextWriter are designed for Unicode character input.



StreamWriter



Writes characters to a Stream, using Encoding to convert characters to bytes.



StringWriter



Writes characters to a String. StringWriter allows you to treat a String with the same API; thus, your output can be either a Stream in any encoding or a String.



BinaryReader



Reads binary data from a stream.



BinaryWriter



Writes binary data to a stream.



Two classes derived from Stream but not listed in Table 1 are offered in other namespaces. The
NetworkStream class represents a Stream over a network connection and resides in the
System.Net.Sockets namespace, and the CryptoStream class links data streams to cryptographic
transformations and resides in the System.Security.Cryptography namespace.

The design of the Stream class and its derivatives is intended to provide a generic view of data sources and destinations so that the developer can interchangeably use any of these classes without redesigning the application. In general, Stream objects are capable of one or more of the following:

  • Reading The transfer of data from a stream into a
    data structure, such as an array of bytes

  • Writing The transfer of data from a data structure
    into a stream

  • Seeking The querying and modifying of the current position within a stream

Note that a given stream might not support all these features. For example, NetworkStream objects don’t support seeking. You can use the CanRead, CanWrite, and CanSeek properties of Stream and its derived classes to determine precisely which operations a given stream does in fact support.

Page
 1 
 2 
 3 
 4 
 5 
 6 
 (Next) 


More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read