[C# – I/O] Streams (Strings, Memory, Compression)

Streams are not restricted to files. You can access the data in memory. Streams are really flexible because they can be chained and you can deal with stream data generically.

 

1. StringReader / StringWriter

What does it mean that you are reading or writing strings? We already have “String” and “StringBuilder” class. Well, “StringReader” and “StringWriter” read and write strings like they are in a file. They implement “TextReader” and “TextWriter” abstract classes. You can use stream methods such as “WriteLine()” or “ReadLine()“.

The “StringWriter” objects write strings into the “StringBuilder” object and the “StringReader” objects read strings from the “String” object.

public static void TestStringStreams()
{
  var sb = new StringBuilder();
  using (var sw = new StringWriter(sb))
  {
    sw.WriteLine("This is a test");
    sw.WriteLine("Hello, World");
  }

  using (var sr = new StringReader(sb.ToString()))
  {
    string s;
    while ((s = sr.ReadLine()) != null)
    {
      Console.WriteLine(s);
    }
  }
}

 

2. MemoryStream

The “MemoryStream” is used to store temporary data in memory . It can read/write only¬†bytes and a byte array. If you want to write or read texts rather than bytes, you can chain the “MemoryStream” object with the “StreamReader” or “StreamWriter” object.

  • int ReadByte()
  • int Read(byte[] buffer, int index, int count)
  • void WriteByte(byte value)
  • void Write(byte[] buffer, int index, int count)
  • void WriteTo(Stream stream)
  • byte[] ToArray()
  • long Seek(long offset, SeekOrigin loc)

With the “WriteTo()” method, you can write data to a different stream such as a file.

public static void TestMemoryStream()
{
  using (var ms = new MemoryStream())
  {
    using (var sw = new StreamWriter(ms))
    {
      sw.WriteLine("This is a test");
      sw.WriteLine("Hello, World");
      sw.Flush();
      ms.Seek(0, SeekOrigin.Begin);
      //ms.WriteTo(File.Create(@"c:tempmemory.txt"));

      using (var sr = new StreamReader(ms))
      {
        string s;
        while ((s = sr.ReadLine()) != null)
        {
          Console.WriteLine(s);
        }
      }
    }
  }
}

 

3. BufferedStream

The “BufferedStream” adds a buffering layer to read and write operations on another stream. You can use this stream just like the “MemoryStream“. Because most files related streams already support buffering, you use the “BufferedStream” rarely.

 

4. Compressed Streams

The .NET Framework provides the functionality to compress and decompress streams.

  • System.IO.Compression.GZipStream
  • System.IO.Compression.DeflateStream

Please look at the more information about compression streams in MSDN (http://msdn.microsoft.com/en-us/library/3z72378a.aspx).

This entry was posted in C# and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s