Explain the stream architecture in .NET


The .NET streams architecture provides a consistent programming interface for reading and writing across different I/O types. It includes classes for manipulating files and directories on disk, as well as specialized streams for compression, named pipes, and memory-mapped files.

The streaming architecture in .NET relies on a backing store and adapters.

Backing Store

It represents a store of data, such as a file or a network connection. It can act as either a source from which bytes can be read sequentially or a destination to which bytes can be written sequentially.

The Stream class in .NET exposes the backing store to programmers. It exposes methods for reading, writing, and positioning. As the name suggests, a stream is flowing data, either a byte at a time or blocks of a certain size. The data doesn't reside all at once in memory, like an array. The stream uses a small amount of memory, regardless of the size of the original data store.

Adapters

Typically, streams deal with bytes. Though bytes are flexible and efficient, applications work with a higher level of abstractions. The adapters such as TextReader/TextWriter, or an XMLReader/XMLWriter wrap a stream and provide access to higher-level data structures such as text strings and XML.

The Stream Class

Stream class is defined in System.IO namespace and provides a generic view of a sequence of bytes. It's an abstract class that acts as a base class for all the streams. It exposes methods for reading, writing, and seeking operations, along with other administrative functions like closing and flushing.

The Stream class has the following members −

Properties

  • CanRead − Returns true, if the current stream supports reading.

  • CanSeek − Returns true, if the current stream supports seeking, i.e. querying and modifying the current position within a stream.

  • CanTimeout − Returns true, if the current stream can time out.

  • CanWrite − Returns true, if the current stream supports writing.

  • Length − Returns the length of the stream as number of bytes.

  • Position − Gets or Sets the position within the current stream.

  • Readtimeout − Gets or sets a value, in milliseconds, that determines how long the stream will attempt to read before timing out.

  • WriteTimeout − Gets or sets a value, in milliseconds, that determines how long the stream will attempt to write before timing out.

Methods

public abstract int Read (byte[] buffer, int offset, int count)

When overridden in a derived class, reads a sequence of bytes from the current stream and advances the position within the stream by the number of bytes read.

public abstract void Write (byte[] buffer, int offset, int count)

When overridden in a derived class, writes a sequence of bytes to the current stream and advances the current position within this stream by the number of bytes written.

public abstract long Seek (long offset, System.IO.SeekOrigin origin);

When overridden in a derived class, sets the position within the current stream.

public abstract void Flush ();

When overridden in a derived class, clears all buffers for this stream and causes any buffered data to be written to the underlying device.

Example

Here is a complete example that demonstrates the usage of Stream.

 Live Demo

using System;
using System.IO;
class Program{
   static void Main(string[] args){
      // Create a file called test.txt in the current directory:
      using (Stream str = new FileStream("sample.txt", FileMode.Create)){
         Console.WriteLine(str.CanRead); // True
         Console.WriteLine(str.CanWrite); // True
         Console.WriteLine(str.CanSeek); // True

         str.WriteByte(75);
         str.WriteByte(76);
         byte[] bytes = { 10, 20, 30 };
         str.Write(bytes, 0, bytes.Length); // Write block of 5 bytes
         Console.WriteLine(str.Length); // 7
         Console.WriteLine(str.Position); // 7
         str.Position = 0; // Move back to the start
         Console.WriteLine(str.ReadByte()); // 101 Console.WriteLine(str.ReadByte()); // 102
     
         // Read from the stream back into the block array:
         Console.WriteLine(str.Read(bytes, 0, bytes.Length)); // 5
         // Assuming the last Read returned 5, we'll be at
         // the end of the file, so Read will now return 0:
         Console.WriteLine(str.Read(bytes, 0, bytes.Length)); // 0
      }
   }
}

Output

True
True
True
5
5
75
76
3
0

Updated on: 19-May-2021

318 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements