TOC

The community is working on translating this tutorial into Amharic, but it seems that no one has started the translation process for this article yet. If you can help us, then please click "More info".

Data Streams:

MemoryStream

The MemoryStream is one of the basic Stream classes which you'll see used quite a bit. It deals with data directly in memory, as the name implies and its often used to deal with bytes coming from another place, e.g. a file or a network location, without locking the source.

So for instance, you may read the entire contents of a file into a MemoryStream, which locks and unlocks the file again immediately, and then start working on the bytes in a MemoryStream. If you need to do a lot of seeking back and forth in the bytes this is also much faster than doing the same directly in e.g. a FileStream because the bytes in a MemoryStream is stored in memory instead of on the disk.

Therefore, you will often see a MemoryStream be initialized with an array of bytes (byte[]) coming from another source, and often, you'll see the instantiated MemoryStream be passed to another mechanism which will utilize the MemoryStream, e.g. one of the StreamReader types. Here's an example:

public void UseMemoryStream()
{
	byte[] fileContents = File.ReadAllBytes("test.txt");
	using(MemoryStream memoryStream = new MemoryStream(fileContents))
	{
		using(TextReader textReader = new StreamReader(memoryStream))
		{
			string line;
			while((line = textReader.ReadLine()) != null)
				Console.WriteLine(line);
		}
	}
}

In this example, we read all the bytes in a simple text file. We then create a MemoryStream with these bytes and then we create a StreamReader instance which will read all the lines from the MemoryStream. As this example illustrates, the MemoryStream is a nice backing source for another class which will do the work, in this case the StreamReader.

Using the StreamReader/StreamWriter classes is a nice shortcut to work with the underlying Stream, but if you insist, you can read the bytes directly out of the MemoryStream and work with them. The MemoryStream class comes with several methods for this, e.g. the ReadByte() method. It will read the byte at the current position, return it and then advance the Position property, preparing the MemoryStream for reading the next byte. Here's an example:

public void UseMemoryStream()
{
	byte[] fileContents = File.ReadAllBytes("test.txt");
	using(MemoryStream memoryStream = new MemoryStream(fileContents))
	{
		int b;
		while((b = memoryStream.ReadByte()) >= 0)
			Console.WriteLine(Convert.ToChar(b));
	}
}

It will read each byte found in the MemoryStream and output it to the Console. Notice that I use the Convert.ToChar() method to turn the byte into an ASCII character, since I know that the source is plaintext.

Summary

The MemoryStream class can be used as the backing source for data you want to keep in memory. This makes it a great temporary storage for data coming from a file or a network resource, to prevent lockups etc. while you work with the data.


This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!