MemoryStream クラス

定義

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

public ref class MemoryStream : System::IO::Stream
public class MemoryStream : System.IO.Stream
[System.Serializable]
public class MemoryStream : System.IO.Stream
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class MemoryStream : System.IO.Stream
type MemoryStream = class
    inherit Stream
[<System.Serializable>]
type MemoryStream = class
    inherit Stream
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type MemoryStream = class
    inherit Stream
Public Class MemoryStream
Inherits Stream
継承
MemoryStream
継承
属性

次のコード例は、バッキング ストアとしてメモリを使用してデータを読み書きする方法を示しています。

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main()
{
   int count;
   array<Byte>^byteArray;
   array<Char>^charArray;
   UnicodeEncoding^ uniEncoding = gcnew UnicodeEncoding;

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

   MemoryStream^ memStream = gcnew 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( "Capacity = {0}, Length = {1}, "
      "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 = gcnew array<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 = gcnew array<Char>(uniEncoding->GetCharCount( byteArray, 0, count ));
      uniEncoding->GetDecoder()->GetChars( byteArray, 0, count, charArray, 0 );
      Console::WriteLine( charArray );
   }
   finally
   {
      memStream->Close();
   }
}
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.GetInvalidPathChars());

        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++] = (byte)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);
        }
    }
}
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.GetInvalidPathChars())

        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

注釈

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

注意

この型は IDisposable インターフェイスを実装しますが、実際に破棄するリソースはありません。 つまり、Dispose() を直接呼び出したり、using (C# の場合) または Using (Visual Basic の場合) といった言語構築を行ってリソースを破棄する必要はありません。

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

オブジェクトが MemoryStream ResX ファイルまたは .resources ファイルに追加された場合は、実行時にメソッドを GetStream 呼び出して取得します。

オブジェクトが MemoryStream リソース ファイルにシリアル化されている場合、実際 UnmanagedMemoryStreamには . この動作により、パフォーマンスが向上し、メソッドを経由 Stream しなくてもデータへのポインターを直接取得できます。

コンストラクター

MemoryStream()

MemoryStream クラスの新しいインスタンスを、0 に初期化される拡張可能な容量を使用して 初期化します。

MemoryStream(Byte[])

指定したバイト配列に基づいて、サイズを変更できない MemoryStream クラスの新しいインスタンスを初期化します。

MemoryStream(Byte[], Boolean)

MemoryStream プロパティを指定どおりに設定し、指定したバイト配列に基づいて、サイズを変更できない CanWrite クラスの新しいインスタンスを初期化します。

MemoryStream(Byte[], Int32, Int32)

バイト配列の指定した領域 (インデックス) に基づいて、サイズを変更できない MemoryStream クラスの新しいインスタンスを初期化します。

MemoryStream(Byte[], Int32, Int32, Boolean)

MemoryStream プロパティを指定どおりに設定し、バイト配列の指定した領域に基づいて、サイズを変更できない CanWrite クラスの新しいインスタンスを初期化します。

MemoryStream(Byte[], Int32, Int32, Boolean, Boolean)

MemoryStream プロパティを指定どおりに設定し、CanWrite を呼び出す機能を指定どおりに設定して、バイト配列の指定した領域に基づき、GetBuffer() クラスの新しいインスタンスを初期化します。

MemoryStream(Int32)

MemoryStream クラスの新しいインスタンスを、指定に従って初期化される拡張可能な容量を使用して初期化します。

プロパティ

CanRead

現在のストリームが読み取りをサポートしているかどうかを示す値を取得します。

CanSeek

現在のストリームがシークをサポートしているかどうかを示す値を取得します。

CanTimeout

現在のストリームがタイムアウトできるかどうかを決定する値を取得します。

(継承元 Stream)
CanWrite

現在のストリームが書き込みをサポートしているかどうかを示す値を取得します。

Capacity

ストリームに割り当てられたバイト数を取得または設定します。

Length

バイト単位のストリーム長を取得します。

Position

ストリームの現在位置を取得または設定します。

ReadTimeout

ストリームがタイムアウト前に読み取りを試みる期間を決定する値 (ミリ秒単位) を取得または設定します。

(継承元 Stream)
WriteTimeout

ストリームがタイムアウト前に書き込みを試行する期間を決定する値 (ミリ秒単位) を取得または設定します。

(継承元 Stream)

メソッド

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

非同期の読み込み動作を開始します。 (代わりに、ReadAsync(Byte[], Int32, Int32, CancellationToken) の使用を検討してください。)

BeginRead(Byte[], Int32, Int32, AsyncCallback, Object)

非同期の読み込み動作を開始します。 (代わりに、ReadAsync(Byte[], Int32, Int32) の使用を検討してください。)

(継承元 Stream)
BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

非同期の書き込み操作を開始します。 (代わりに、WriteAsync(Byte[], Int32, Int32, CancellationToken) の使用を検討してください。)

BeginWrite(Byte[], Int32, Int32, AsyncCallback, Object)

非同期の書き込み操作を開始します。 (代わりに、WriteAsync(Byte[], Int32, Int32) の使用を検討してください。)

(継承元 Stream)
Close()

読み取りと書き込み用のストリームを閉じます。

Close()

現在のストリームを閉じ、現在のストリームに関連付けられているすべてのリソース (ソケット、ファイル ハンドルなど) を解放します。 このメソッドを呼び出す代わりに、ストリームが適切に破棄されていることを確認します。

(継承元 Stream)
CopyTo(Stream)

現在のストリームからバイトを読み取り、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CopyTo(Stream, Int32)

指定したバッファー サイズを使用して、現在のメモリ ストリームからバイトを読み取り、別のストリームに書き込みます。

CopyTo(Stream, Int32)

指定されたバッファー サイズを使用して、現在のストリームからバイトを読み取り、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CopyToAsync(Stream)

現在のストリームからすべてのバイトを非同期に読み取り、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CopyToAsync(Stream, CancellationToken)

指定されたバッファー サイズを使用して、現在のストリームからバイトを非同期に読み取り、指定されたキャンセル トークンを使用して、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CopyToAsync(Stream, Int32)

指定されたバッファー サイズを使用して、現在のストリームからバイトを非同期に読み取り、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CopyToAsync(Stream, Int32, CancellationToken)

指定されたバッファー サイズを使用して、現在のストリームからすべてのバイトを非同期に読み取り、指定されたバッファー サイズとキャンセル トークンを使用して、別のストリームに書き込みます。

CopyToAsync(Stream, Int32, CancellationToken)

指定されたバッファー サイズを使用して、現在のストリームからバイトを非同期に読み取り、指定されたバッファー サイズとキャンセル トークンを使用して、別のストリームに書き込みます。 両方のストリーム位置は、コピーされたバイト数だけ進みます。

(継承元 Stream)
CreateObjRef(Type)

リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。

(継承元 MarshalByRefObject)
CreateWaitHandle()
互換性のために残されています。
互換性のために残されています。
互換性のために残されています。

WaitHandle オブジェクトを割り当てます。

(継承元 Stream)
Dispose()

Stream によって使用されているすべてのリソースを解放します。

(継承元 Stream)
Dispose(Boolean)

MemoryStream クラスによって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

Dispose(Boolean)

Stream によって使用されているアンマネージド リソースを解放し、オプションでマネージド リソースも解放します。

(継承元 Stream)
DisposeAsync()

Stream によって使用されているアンマネージ リソースを非同期に解放します。

(継承元 Stream)
EndRead(IAsyncResult)

保留中の非同期読み取りが完了するまで待機します。 (代わりに、ReadAsync(Byte[], Int32, Int32, CancellationToken) の使用を検討してください。)

EndRead(IAsyncResult)

保留中の非同期読み取りが完了するまで待機します。 (代わりに、ReadAsync(Byte[], Int32, Int32) の使用を検討してください。)

(継承元 Stream)
EndWrite(IAsyncResult)

非同期書き込み操作を終了します。 (代わりに、WriteAsync(Byte[], Int32, Int32, CancellationToken) の使用を検討してください。)

EndWrite(IAsyncResult)

非同期書き込み操作を終了します。 (代わりに、WriteAsync(Byte[], Int32, Int32) の使用を検討してください。)

(継承元 Stream)
Equals(Object)

指定されたオブジェクトが現在のオブジェクトと等しいかどうかを判断します。

(継承元 Object)
Flush()

アクションが実行されないように、Flush() メソッドをオーバーライドします。

FlushAsync()

ストリームに対応するすべてのバッファーを非同期にクリアし、バッファー内のデータを基になるデバイスに書き込みます。

(継承元 Stream)
FlushAsync(CancellationToken)

このストリームのすべてのバッファーを非同期的にクリアし、キャンセル要求を監視します。

FlushAsync(CancellationToken)

ストリームに対応するすべてのバッファーを非同期にクリアし、バッファー内のデータを基になるデバイスに書き込み、キャンセル要求を監視します。

(継承元 Stream)
GetBuffer()

このストリームの作成元の符号なしバイトの配列を返します。

GetHashCode()

既定のハッシュ関数として機能します。

(継承元 Object)
GetLifetimeService()
互換性のために残されています。

対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
GetType()

現在のインスタンスの Type を取得します。

(継承元 Object)
InitializeLifetimeService()
互換性のために残されています。

このインスタンスの有効期間ポリシーを制御する有効期間サービス オブジェクトを取得します。

(継承元 MarshalByRefObject)
MemberwiseClone()

現在の Object の簡易コピーを作成します。

(継承元 Object)
MemberwiseClone(Boolean)

現在の MarshalByRefObject オブジェクトの簡易コピーを作成します。

(継承元 MarshalByRefObject)
ObjectInvariant()

この API は製品インフラストラクチャをサポートします。コードから直接使用するものではありません。

Contract のサポートを提供します。

ObjectInvariant()
互換性のために残されています。

Contract のサポートを提供します。

(継承元 Stream)
Read(Byte[], Int32, Int32)

現在のストリームからバイトのブロックを読み取り、データをバッファーに書き込みます。

Read(Span<Byte>)

現在のメモリ ストリームからバイト シーケンスを読み取り、読み取ったバイト数だけメモリ ストリーム内の位置を進めます。

Read(Span<Byte>)

派生クラスによってオーバーライドされた場合は、現在のストリームからバイト シーケンスを読み取り、読み取ったバイト数の分だけストリームの位置を進めます。

(継承元 Stream)
ReadAsync(Byte[], Int32, Int32)

現在のストリームからバイト シーケンスを非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進めます。

(継承元 Stream)
ReadAsync(Byte[], Int32, Int32, CancellationToken)

現在のストリームからバイト シーケンスを非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進め、キャンセル要求を監視します。

ReadAsync(Byte[], Int32, Int32, CancellationToken)

現在のストリームからバイト シーケンスを非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進め、キャンセル要求を監視します。

(継承元 Stream)
ReadAsync(Memory<Byte>, CancellationToken)

現在のメモリ ストリームからバイト シーケンスを非同期に読み取り、シーケンスを destination に書き込み、読み取ったバイト数だけメモリ ストリーム内の位置を進め、キャンセル要求を監視します。

ReadAsync(Memory<Byte>, CancellationToken)

現在のストリームからバイト シーケンスを非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進め、キャンセル要求を監視します。

(継承元 Stream)
ReadAtLeast(Span<Byte>, Int32, Boolean)

現在のストリームから少なくとも最小バイト数を読み取り、ストリーム内の位置を読み取ったバイト数だけ進めます。

(継承元 Stream)
ReadAtLeastAsync(Memory<Byte>, Int32, Boolean, CancellationToken)

現在のストリームから少なくとも最小バイト数を非同期に読み取り、読み取ったバイト数でストリーム内の位置を進め、取り消し要求を監視します。

(継承元 Stream)
ReadByte()

現在のストリームからバイトを読み取ります。

ReadExactly(Byte[], Int32, Int32)

現在のストリームからバイト数を読み取 count り、ストリーム内の位置を進めます。

(継承元 Stream)
ReadExactly(Span<Byte>)

現在のストリームからバイトを読み取り、入力されるまでストリーム内の位置を buffer 進めます。

(継承元 Stream)
ReadExactlyAsync(Byte[], Int32, Int32, CancellationToken)

現在のストリームからバイト数を非同期に読み取り count 、ストリーム内の位置を進め、取り消し要求を監視します。

(継承元 Stream)
ReadExactlyAsync(Memory<Byte>, CancellationToken)

現在のストリームからバイトを非同期に読み取り、入力されるまでストリーム内の位置を buffer 進め、取り消し要求を監視します。

(継承元 Stream)
Seek(Int64, SeekOrigin)

現在のストリーム内の位置を指定した値に設定します。

SetLength(Int64)

現在のストリーム長を指定した値に設定します。

ToArray()

Position プロパティには関係なく、ストリームの内容をバイト配列に書き込みます。

ToString()

現在のオブジェクトを表す文字列を返します。

(継承元 Object)
TryGetBuffer(ArraySegment<Byte>)

このストリームの作成元の符号なしバイトの配列を返します。 戻り値は、変換が成功したかどうかを示します。

Write(Byte[], Int32, Int32)

バッファーから読み取ったデータを使用して、現在のストリームにバイトのブロックを書き込みます。

Write(ReadOnlySpan<Byte>)

source に含まれるバイト シーケンスを現在のメモリ ストリームに書き込み、書き込まれたバイト数だけ、このメモリ ストリーム内の現在の位置を進めます。

Write(ReadOnlySpan<Byte>)

派生クラスによってオーバーライドされた場合は、現在のストリームにバイト シーケンスを書き込み、書き込んだバイト数の分だけストリームの現在位置を進めます。

(継承元 Stream)
WriteAsync(Byte[], Int32, Int32)

現在のストリームにバイト シーケンスを非同期に書き込み、書き込んだバイト数だけストリーム内の現在位置を進めます。

(継承元 Stream)
WriteAsync(Byte[], Int32, Int32, CancellationToken)

現在のストリームにバイト シーケンスを非同期に書き込み、書き込んだバイト数だけストリーム内の現在位置を進め、キャンセル要求を監視します。

WriteAsync(Byte[], Int32, Int32, CancellationToken)

現在のストリームにバイト シーケンスを非同期に書き込み、書き込んだバイト数だけストリーム内の現在位置を進め、キャンセル要求を監視します。

(継承元 Stream)
WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

source に含まれるバイト シーケンスを現在のメモリ ストリームに非同期で書き込み、書き込まれたバイト数だけ、このメモリ ストリーム内の現在の位置を進め、キャンセル要求を監視します。

WriteAsync(ReadOnlyMemory<Byte>, CancellationToken)

現在のストリームにバイト シーケンスを非同期に書き込み、書き込んだバイト数だけストリーム内の現在位置を進め、キャンセル要求を監視します。

(継承元 Stream)
WriteByte(Byte)

現在のストリーム内の現在位置に 1 バイトを書き込みます。

WriteTo(Stream)

メモリ ストリームの内容全体を別のストリームに書き込みます。

明示的なインターフェイスの実装

IDisposable.Dispose()

Stream によって使用されているすべてのリソースを解放します。

(継承元 Stream)

拡張メソッド

AsInputStream(Stream)

Windows ストア アプリ用 .NET のマネージド ストリームを Windows ランタイムの入力ストリームに変換します。

AsOutputStream(Stream)

Windows ストア アプリ用 .NET のマネージド ストリームを Windows ランタイムの出力ストリームに変換します。

AsRandomAccessStream(Stream)

特定のストリームをランダム アクセス ストリームに変換します。

GetWindowsRuntimeBuffer(MemoryStream)

指定したメモリ ストリームと同じメモリを表す Windows.Storage.Streams.IBuffer インターフェイスを返します。

GetWindowsRuntimeBuffer(MemoryStream, Int32, Int32)

指定したメモリ ストリームが示すメモリ内の領域を表す Windows.Storage.Streams.IBuffer インターフェイスを返します。

ConfigureAwait(IAsyncDisposable, Boolean)

非同期の破棄可能から返されるタスク上での待機がどのように実行されるかを構成します。

適用対象

こちらもご覧ください