NGWS SDK Documentation  

This is preliminary documentation and subject to change.
To comment on this topic, please send us email at ngwssdk@microsoft.com. Thanks!

Composing Streams

A backing store is a storage medium, such as a disk or memory. All the different backing stores implement their own stream as an implementation of the Stream class. Each stream type reads and writes bytes from and to its given backing store. Streams that interface to backing stores are called base streams. Base streams have constructors that have the parameters necessary to connect the stream to the backing store. For example, a file stream might have a parameter that is a file path or file handle.

The design of the System.IO classes provides simplified stream composing. Base streams can be attached to one or more pass-through streams that provide the desired functionality. A reader or writer can be attached to the end of the chain so that types other than bytes can be read or written easily.

The following example composes a stream with buffering and then reads from it.

public static void Main(String[] args)
{
File f = new File(“MyFile.text”);
FileStream s = File.Open(FileMode.Read);
BufferedStream b = new BufferedStream(s);
StreamReader t = new StreamReader(b);
String s = t.ReadLine();
b.Close();
}

This example creates a File object that points to MyFile.text. Next, a FileStream is created through a call to File.Open, returning a FileStream object. A buffer is then composed by creating a BufferedStream and passing the file stream to it as its constructor argument. A TextReader is then created and passed the BufferedStream as input. Finally, the user reads a line from the file by calling the ReadLine method of the TextReader.