MemoryStream クラス

バッキング ストアとしてメモリを使用するストリームを作成します。

この型のすべてのメンバの一覧については、MemoryStream メンバ を参照してください。

System.Object
   System.MarshalByRefObject
      System.IO.Stream
         System.IO.MemoryStream

<Serializable>
Public Class MemoryStream   Inherits Stream
[C#]
[Serializable]
public class MemoryStream : Stream
[C++]
[Serializable]
public __gc class MemoryStream : public Stream
[JScript]
public
   Serializable
class MemoryStream extends Stream

スレッドセーフ

この型の public static (Visual Basicでは Shared) のすべてのメンバは、マルチスレッド操作で安全に使用できます。インスタンスのメンバの場合は、スレッドセーフであるとは限りません。

解説

ファイルの作成およびテキストのファイルへの書き込みの例については、「 ファイルへのテキストの書き込み 」を参照してください。ファイルからのテキストの読み取りの例については、「 ファイルからのテキストの読み取り 」を参照してください。バイナリ ファイルからの読み取りおよびバイナリ ファイルへの書き込みの例については、「 新しく作成したデータ ファイルの読み取りと書き込み 」を参照してください。

MemoryStream クラスは、バッキング ストアとしてディスクやネットワーク接続ではなく、メモリを使用するストリームを作成します。 MemoryStream は、 MemoryStream オブジェクトの作成時に初期化される符号なしバイト配列として格納されるデータをカプセル化します。配列は空の配列としても作成できます。カプセル化されたデータは、メモリで直接アクセスできます。メモリ ストリームを使用すると、アプリケーションでの一時バッファと一時ファイルの必要性を減らすことができます。

ストリームの current position は、次の読み取りまたは書き込み操作が実行される位置です。現在の位置は、 Seek メソッドで取得または設定できます。 MemoryStream の新しいインスタンスを作成するときに、現在の位置はゼロに設定されます。

符号なしバイト配列で作成したメモリ ストリームには、サイズを変更できないデータのストリーム ビュー機能が用意されています。これは書き込み専用です。バイト配列を使用する場合、ストリームに追加したり、ストリームを縮小したりできません。ただし、コンストラクタに渡すパラメータによっては、既存の内容を変更できることがあります。空のメモリ ストリームは、サイズの変更、書き込み、および読み取りを行うことができます。

使用例

[Visual Basic, C#, C++] メモリをバッキング ストアとして使用してデータを読み書きする方法の例を次に示します。

 
Imports System
Imports System.IO
Imports System.Text

Module MemStream

    Sub Main()
    
        Dim count As Integer
        Dim byteArray As Byte()
        Dim charArray As Char()
        Dim uniEncoding As New UnicodeEncoding()

        ' Create the data to write to the stream.
        Dim firstString As Byte() = _
            uniEncoding.GetBytes("Invalid file path characters are: ")
        Dim secondString As Byte() = _
            uniEncoding.GetBytes(Path.InvalidPathChars)

        Dim memStream As New MemoryStream(100)
        Try
            ' Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length)

            ' Write the second string to the stream, byte by byte.
            count = 0
            While(count < secondString.Length)
                memStream.WriteByte(secondString(count))
                count += 1
            End While
            
            ' Write the stream properties to the console.
            Console.WriteLine( _
                "Capacity = {0}, Length = {1}, Position = {2}", _
                memStream.Capacity.ToString(), _
                memStream.Length.ToString(), _
                memStream.Position.ToString())

            ' Set the stream position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin)

            ' Read the first 20 bytes from the stream.
            byteArray = _
                New Byte(CType(memStream.Length, Integer)){}
            count = memStream.Read(byteArray, 0, 20)

            ' Read the remaining Bytes, Byte by Byte.
            While(count < memStream.Length)
                byteArray(count) = _
                    Convert.ToByte(memStream.ReadByte())
                count += 1
            End While

            ' Decode the Byte array into a Char array 
            ' and write it to the console.
            charArray = _
                New Char(uniEncoding.GetCharCount( _
                byteArray, 0, count)){}
            uniEncoding.GetDecoder().GetChars( _
                byteArray, 0, count, charArray, 0)
            Console.WriteLine(charArray)
        Finally
            memStream.Close()
        End Try

    End Sub
End Module

[C#] 
using System;
using System.IO;
using System.Text;

class MemStream
{
    static void Main()
    {
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();

        // Create the data to write to the stream.
        byte[] firstString = uniEncoding.GetBytes(
            "Invalid file path characters are: ");
        byte[] secondString = uniEncoding.GetBytes(
            Path.InvalidPathChars);

        using(MemoryStream memStream = new MemoryStream(100))
        {
            // Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length);

            // Write the second string to the stream, byte by byte.
            count = 0;
            while(count < secondString.Length)
            {
                memStream.WriteByte(secondString[count++]);
            }

            // Write the stream properties to the console.
            Console.WriteLine(
                "Capacity = {0}, Length = {1}, Position = {2}\n",
                memStream.Capacity.ToString(), 
                memStream.Length.ToString(), 
                memStream.Position.ToString());

            // Set the position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin);

            // Read the first 20 bytes from the stream.
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);

            // Read the remaining bytes, byte by byte.
            while(count < memStream.Length)
            {
                byteArray[count++] = 
                    Convert.ToByte(memStream.ReadByte());
            }

            // Decode the byte array into a char array 
            // and write it to the console.
            charArray = new char[uniEncoding.GetCharCount(
                byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(
                byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray);
        }
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;

void main()
{
    int count;
    Byte byteArray __gc[];
    Char charArray __gc[];
    UnicodeEncoding* uniEncoding = new UnicodeEncoding();

    // Create the data to write to the stream.
    Byte firstString __gc[] = 
        uniEncoding->GetBytes(S"Invalid file path characters are: ");
    Byte secondString __gc[] = 
        uniEncoding->GetBytes(Path::InvalidPathChars);

    MemoryStream* memStream = new MemoryStream(100);
    try
    {
        // Write the first string to the stream.
        memStream->Write(firstString, 0 , firstString->Length);

        // Write the second string to the stream, byte by byte.
        count = 0;
        while(count < secondString->Length)
        {
            memStream->WriteByte(secondString[count++]);
        }

        // Write the stream properties to the console.
        Console::WriteLine(S"Capacity = {0}, Length = {1}, "
            S"Position = {2}\n", memStream->Capacity.ToString(), 
            memStream->Length.ToString(), 
            memStream->Position.ToString());

        // Set the stream position to the beginning of the stream.
        memStream->Seek(0, SeekOrigin::Begin);

        // Read the first 20 bytes from the stream.
        byteArray = new Byte __gc[memStream->Length];
        count = memStream->Read(byteArray, 0, 20);

        // Read the remaining bytes, byte by byte.
        while(count < memStream->Length)
        {
            byteArray[count++] = 
                Convert::ToByte(memStream->ReadByte());
        }

        // Decode the Byte array into a Char array 
        // and write it to the console.
        charArray = 
            new Char __gc[uniEncoding->GetCharCount(
            byteArray, 0, count)];
        uniEncoding->GetDecoder()->GetChars(
            byteArray, 0, count, charArray, 0);
        Console::WriteLine(charArray);
    }
    __finally
    {
        memStream->Close();
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

名前空間: System.IO

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET

アセンブリ: Mscorlib (Mscorlib.dll 内)

参照

MemoryStream メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み