MemoryStream クラス
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
バッキング ストアがメモリであるストリームを作成します。
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
- 継承
- 継承
- 属性
例
次のコード例は、バッキング ストアとしてメモリを使用してデータを読み書きする方法を示しています。
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 クラスの新しいインスタンスを初期化します。 |
| 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) |
現在のメモリ ストリームからバイト シーケンスを非同期に読み取り、シーケンスを |
| ReadAsync(Memory<Byte>, CancellationToken) |
現在のストリームからバイト シーケンスを非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進め、取り消し要求を監視します。 (継承元 Stream) |
| ReadAtLeast(Span<Byte>, Int32, Boolean) |
現在のストリームから少なくともバイト数を読み取り、読み取ったバイト数だけストリーム内の位置を進めます。 (継承元 Stream) |
| ReadAtLeastAsync(Memory<Byte>, Int32, Boolean, CancellationToken) |
現在のストリームから少なくとも最小バイト数を非同期に読み取り、読み取ったバイト数だけストリーム内の位置を進め、キャンセル要求を監視します。 (継承元 Stream) |
| ReadByte() |
現在のストリームからバイトを読み取ります。 |
| ReadExactly(Byte[], Int32, Int32) |
現在のストリーム |
| ReadExactly(Span<Byte>) |
現在のストリームからバイトを読み取り、 |
| ReadExactlyAsync(Byte[], Int32, Int32, CancellationToken) |
現在のストリーム |
| ReadExactlyAsync(Memory<Byte>, CancellationToken) |
現在のストリームからバイトを非同期に読み取り、 |
| Seek(Int64, SeekOrigin) |
現在のストリーム内の位置を指定した値に設定します。 |
| SetLength(Int64) |
現在のストリームの長さを指定した値に設定します。 |
| ToArray() |
Position プロパティに関係なく、ストリームの内容をバイト配列に書き込みます。 |
| ToString() |
現在のオブジェクトを表す文字列を返します。 (継承元 Object) |
| TryGetBuffer(ArraySegment<Byte>) |
このストリームの作成元の符号なしバイトの配列を返します。 戻り値は、変換が成功したかどうかを示します。 |
| Write(Byte[], Int32, Int32) |
バッファーから読み取られたデータを使用して、現在のストリームにバイト ブロックを書き込みます。 |
| Write(ReadOnlySpan<Byte>) |
|
| Write(ReadOnlySpan<Byte>) |
派生クラスでオーバーライドされると、現在のストリームにバイト シーケンスを書き込み、書き込まれたバイト数だけこのストリーム内の現在の位置を進めます。 (継承元 Stream) |
| WriteAsync(Byte[], Int32, Int32) |
現在のストリームにバイト シーケンスを非同期に書き込み、書き込まれたバイト数だけこのストリーム内の現在位置を進めます。 (継承元 Stream) |
| WriteAsync(Byte[], Int32, Int32, CancellationToken) |
現在のストリームにバイトシーケンスを非同期に書き込み、書き込まれたバイト数だけこのストリーム内の現在位置を進め、キャンセル要求を監視します。 |
| WriteAsync(Byte[], Int32, Int32, CancellationToken) |
現在のストリームにバイトシーケンスを非同期に書き込み、書き込まれたバイト数だけこのストリーム内の現在位置を進め、キャンセル要求を監視します。 (継承元 Stream) |
| WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
|
| WriteAsync(ReadOnlyMemory<Byte>, CancellationToken) |
現在のストリームにバイトシーケンスを非同期に書き込み、書き込まれたバイト数だけこのストリーム内の現在位置を進め、キャンセル要求を監視します。 (継承元 Stream) |
| WriteByte(Byte) |
現在の位置にある現在のストリームにバイトを書き込みます。 |
| WriteTo(Stream) |
このメモリ ストリームの内容全体を別のストリームに書き込みます。 |
明示的なインターフェイスの実装
| IDisposable.Dispose() |
Streamで使用されているすべてのリソースを解放します。 (継承元 Stream) |
拡張メソッド
| CopyToAsync(Stream, PipeWriter, CancellationToken) |
キャンセル トークンを使用して、Stream からバイトを非同期に読み取り、指定した PipeWriterに書き込みます。 |
| AsInputStream(Stream) |
.NET for Windows ストア アプリのマネージド ストリームを Windows ランタイムの入力ストリームに変換します。 |
| AsOutputStream(Stream) |
.NET for Windows ストア アプリのマネージド ストリームを Windows ランタイムの出力ストリームに変換します。 |
| AsRandomAccessStream(Stream) |
指定したストリームをランダム アクセス ストリームに変換します。 |
| GetWindowsRuntimeBuffer(MemoryStream) |
指定したメモリ ストリームと同じメモリを表す Windows.Storage.Streams.IBuffer インターフェイスを返します。 |
| GetWindowsRuntimeBuffer(MemoryStream, Int32, Int32) |
指定したメモリ ストリームが表すメモリ内の領域を表す Windows.Storage.Streams.IBuffer インターフェイスを返します。 |
| ConfigureAwait(IAsyncDisposable, Boolean) |
非同期破棄から返されるタスクの待機を実行する方法を構成します。 |
適用対象
こちらもご覧ください
- ファイルおよびストリーム I/O
- 方法: ファイル からテキストを読み取る
- 方法: ファイル にテキストを書き込む