Using MemoryStream


Sometimes it is useful to read input from or to write output to an array, rather than directly from or to a device. To do this, you will use MemoryStream. MemoryStream is an implementation of Stream that uses an array of bytes for input and/or output. Here is one of the constructors that it defines:

 MemoryStream(byte[ ] buf) 

Here, buf is an array of bytes that will be used for the source and/or target of I/O requests. The stream created by this constructor can be written or read, and supports Seek( ). You must remember to make buf large enough to hold whatever output you will be directing to it.

Here is a program that demonstrates the use of MemoryStream:

 // Demonstrate MemoryStream. using System; using System.IO; class MemStrDemo {   public static void Main() {     byte[] storage = new byte[255];     // Create a memory-based stream.     MemoryStream memstrm = new MemoryStream(storage);     // Wrap memstrm in a reader and a writer.     StreamWriter memwtr = new StreamWriter(memstrm);     StreamReader memrdr = new StreamReader(memstrm);     // Write to storage, through memwtr.     for(int i=0; i < 10; i++)        memwtr.WriteLine("byte [" + i + "]: " + i);     // put a period at the end     memwtr.WriteLine(".");     memwtr.Flush();     Console.WriteLine("Reading from storage directly: ");     // Display contents of storage directly.     foreach(char ch in storage) {       if (ch == '.') break;       Console.Write(ch);     }     Console.WriteLine("\nReading through memrdr: ");     // Read from memstrm using the stream reader.     memstrm.Seek(0, SeekOrigin.Begin); // reset file pointer     string str = memrdr.ReadLine();     while(str != null) {       Console.WriteLine(str);       str = memrdr.ReadLine();       if(str.CompareTo(".") == 0) break;     }   } }

The output from the program is shown here:

 Reading from storage directly: byte [0]: 0 byte [1]: 1 byte [2]: 2 byte [3]: 3 byte [4]: 4 byte [5]: 5 byte [6]: 6 byte [7]: 7 byte [8]: 8 byte [9]: 9 Reading through memrdr: byte [0]: 0 byte [1]: 1 byte [2]: 2 byte [3]: 3 byte [4]: 4 byte [5]: 5 byte [6]: 6 byte [7]: 7 byte [8]: 8 byte [9]: 9

In the program, an array of bytes called storage is created. This array is then used as the underlying storage for a MemoryStream called memstrm. From memstrm are created a StreamReader called memrdr and a StreamWriter called memwtr. Using memwtr, output is written to the memory-based stream. Notice that after the output has been written, Flush( ) is called on memwtr. This is necessary to ensure that the contents of memwtr’s buffer are actually written to the underlying array. Next, the contents of the underlying byte array are displayed manually, using a foreach loop. Then, using Seek( ), the file pointer is reset to the start of the stream, and the memory stream is read using memrdr.

Memory-based streams are quite useful in programming. For example, you can construct complicated output in advance, storing it in the array until it is needed. This technique is especially useful when programming for a GUI environment, such as Windows. You can also redirect a standard stream to read from an array. This might be useful for feeding test information into a program, for example.




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net