[C#] Handling Strings as a Stream (MemoryStream)

There are situations where you want to test a method that accepts a file, but creating a physical file is a hassle. You might also want to process string data received from a network in the same way you would handle a file.

In such cases, you can convert a string into a byte array and pass it to a MemoryStream. This allows you to treat the string data as if it were a file stream.

目次

Table of Contents

  • Implementation Sample: Reading a String as a File
  • Explanation and Benefits
    1. The Role of MemoryStream
    1. Improved Testability
    1. Be Careful with Character Encoding

Implementation Sample: Reading a String as a File

In the following code, multi-line string data is converted into a MemoryStream. We then use a StreamReader to read and process it line by line.

Sample Code

using System;
using System.IO;
using System.Text;

public class Program
{
    public static void Main()
    {
        // 1. Multi-line string for testing
        // Define data as a string that would normally be in a file
        string dummyData = @"[ServerConfig]
Host=192.170.1.10
Port=8080
Timeout=300
Environment=Production";

        Console.WriteLine("--- Stream processing started ---");

        // 2. Create a StreamReader from the string using a helper method
        using (var reader = CreateStreamReader(dummyData))
        {
            // This can be handled exactly like reading a file
            while (!reader.EndOfStream)
            {
                var line = reader.ReadLine();
                // Format and output the line
                Console.WriteLine($"Read line: <{line}>");
            }
        }
    }

    /// <summary>
    /// Helper method to create a StreamReader based on a string
    /// </summary>
    static StreamReader CreateStreamReader(string inputString)
    {
        // A. Convert the string to a byte array (UTF-8)
        byte[] byteArray = Encoding.UTF8.GetBytes(inputString);

        // B. Expand the byte array into a stream in memory (MemoryStream)
        // This makes it behave as a Stream without file I/O
        var stream = new MemoryStream(byteArray);

        // C. Create and return a Reader to read the stream
        return new StreamReader(stream);
    }
}

Execution Result

--- Stream processing started ---
Read line: <[ServerConfig]>
Read line: <Host=192.170.1.10>
Read line: <Port=8080>
Read line: <Timeout=300>
Read line: <Environment=Production>

Explanation and Benefits

1. The Role of MemoryStream

MemoryStream is a class for reading and writing data in a “byte array in memory” rather than a file on a disk. By using the constructor new MemoryStream(byte[] buffer), you can immediately create a stream that wraps existing data.

2. Improved Testability

When writing unit tests for classes that perform file operations, using File.Open makes tests slow and unstable because you have to prepare and clean up actual files. If you design your methods to accept the abstract Stream class instead of FileStream, you can use this technique to pass a MemoryStream. This allows you to verify your logic without any actual file access.

3. Be Careful with Character Encoding

In this example, we use Encoding.UTF8.GetBytes(str) to convert the string to a byte array. The StreamReader also reads data as UTF-8 by default. If you need to simulate data in other encodings, such as Shift-JIS, you must ensure the encoding matches both during the byte conversion and when creating the StreamReader.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次