C#. Stream concept. Stream architecture in C#. Basic storage streams. Streams with decorators. Adapters streams.

Stream concept. Stream architecture in C#. Basic storage streams. Streams with decorators. Adapters streams


Contents


Search other websites:




1. What is stream in programming? Stream concept

In programming, a stream is a logical device that provides:

  • consumption (receipt) of information. In this case, the term input stream is defined;
  • production (transmission) of information. In this case, the term output stream is defined.

Stream is an abstraction that provides input/output of information in a program. The I/O system associates a stream with a physical device (Figure 1). An input or output stream contains the same set of commands regardless of the physical device. So, for example, output to a printer or screen is done by the same function calls, or output to the console works the same as output to a file. In turn, the same function can work with different types of physical devices.

C#. .NET. Interaction of a stream with various types of physical input/output devices (printer, remote computer, file)

Figure 1. Interaction of a stream with various types of physical input/output devices (printer, remote computer, file)

 

2. The architecture of .NET streams. Streams categories

In .NET technology, streams are divided into two main categories (Figure 2):

  • streams with basic storages;
  • streams with decorators.

Streams with basic storages implement a specific kind of storage, which can be:

  • file;
  • memory;
  • network;
  • isolated storage.

Decorated streams are used to modify data passed to basic storages. Examples of this modification include:

  • data encryption before sending on the network;
  • data archiving;
  • data compression and unpacking by known methods;
  • data buffering.

To modify an existing stream, streams with decorators use the approach of the Decorator pattern. Similar schemes for using the Decorator pattern are used in other programming languages (for example, Java).

Architecture of streams in .NET

Figure 2. Architecture of streams in .NET

Both categories of streams work exclusively with bytes. Stream adapters are used to represent bytes in human-readable text.

 

3. Streams with basic storages. Overview

Streams with basic storages are associated with a specific type of storage: files, memory, network, and the like. Basic streams with backing stores are represented by the following classes:

  • FileStream is a class that provides a stream for a file. The class contains a variety of file handling facilities. These tools provide both synchronous and asynchronous reading from a file and synchronous and asynchronous writing to a file;
  • IsolatedStorage is an abstract class that serves as the base for classes that provide access to isolated storage for files;
  • MemoryStream is a class designed to handle streams that reside in memory;
  • NetworkStream is a class that contains tools of representing a stream of data on a network.

 

4. Decorated streams. Overview

Decorated streams implement the modification (transformation) of the transmitted data into the basic storages for storage or other use. Decorated streams use the Decorator pattern to modify an existing data stream as needed. The following are the main classes that provide decorated streams:

  • BufferedStream is a class that contains buffers for reading data from a stream and writing data to a stream. Reading/writing of data is carried out through a buffer – a piece of memory of a given size;
  • DeflateStream is a class that provides methods for compressing and decompressing data streams. The class uses the Deflate lossless compression algorithm;
  • GZipStream – a class that implements methods and properties for compressing / decompressing stream data based on the GZip data format specification;
  • CryptoStream – a class that performs cryptographic transformations on a data stream.

Decorated streams are a separate class section in the .NET architecture. This presentation provides the following advantages:

  • decorated streams separate encryption, compression, and other operations from those used in streams of basic storages;
  • using streams with decorators frees streams with basic stores from the need to perform encryption, compression, etc.;
  • decorating streams does not depend on changing the interface in the program;
  • decorator streams can be connected at runtime;
  • support for the Decorator pattern makes it possible to chain decorators (for example, encryption + compression).

 

5. Stream adapters. The purpose. Overview

Stream adapters are at a higher level of program interaction. They allow you to convert byte streams (streams with decorators, streams with basic storages) to a specific format.

Stream adapters work the same way: they wrap a byte stream in an adapter class wrapper with appropriate methods. These methods convert the byte data stream to the desired format (for example, obtain an XML data format).

The following are the main classes related to stream adapters:

  • TextReader is an abstract class that can read character sequences;
  • TextWriter is an abstract class that can write sequences of characters;
  • StreamReader – a class that implements TextReader;
  • StreamWriter – a class that implements the abstract TextWriter class. The class contains tools for writing characters to a stream in a given encoding;
  • BinaryReader – a class containing methods for reading primitive data types (int, float, double, etc.) in the specified encoding system;
  • BinaryWriter – a class that implements methods for writing primitive data types (int, float, byte, etc.) and strings in the specified encoding system;
  • XmlReader is a class that contains tools for uncached XML data reading;
  • XmlWriter is a class that contains tools for writing streams or files with XML data.

 


Related topics