MemoryStream クラス
アセンブリ: mscorlib (mscorlib.dll 内)
構文
ファイルの作成およびテキストのファイルへの書き込みの例については、「方法 : ファイルにテキストを書き込む」を参照してください。ファイルからのテキストの読み取りの例については、「方法 : ファイルからテキストを読み取る」を参照してください。バイナリ ファイルの読み取りおよび書き込みの例については、「方法 : 新しく作成されたデータ ファイルに対して読み書きする」を参照してください。
MemoryStream クラスは、バッキング ストアとしてディスクやネットワーク接続ではなく、メモリを使用するストリームを作成します。MemoryStream は、MemoryStream オブジェクトの作成時に初期化される符号なしバイト配列として格納されるデータをカプセル化します。配列は空の配列としても作成できます。カプセル化されたデータは、メモリで直接アクセスできます。メモリ ストリームを使用すると、アプリケーションでの一時バッファと一時ファイルの必要性を減らすことができます。
ストリームの current position は、次の読み取りまたは書き込み操作が実行される位置です。現在の位置は、Seek メソッドで取得または設定できます。MemoryStream の新しいインスタンスを作成するときに、現在の位置はゼロに設定されます。
符号なしバイト配列で作成したメモリ ストリームには、サイズを変更できないデータのストリーム ビュー機能が用意されています。これは書き込み専用です。バイト配列を使用する場合、ストリームに追加したり、ストリームを縮小したりできません。ただし、コンストラクタに渡すパラメータによっては、既存の内容を変更できることがあります。空のメモリ ストリームは、サイズの変更、書き込み、および読み取りを行うことができます。
MemoryStream オブジェクトを ResX ファイルまたは .resources ファイルに追加した場合、実行時に GetStream メソッドを呼び出してこのオブジェクトを取得します。
MemoryStream オブジェクトがリソース ファイルにシリアル化される場合、このオブジェクトは実際には UnmanagedMemoryStream としてシリアル化されます。これにより、パフォーマンスが向上するだけでなく、Stream メソッドを使用せずにデータへのポインタを直接取得できます。
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : Windows CE では、クリップボードから貼り付けられたメモリ ストリームは、クリップボードにコピーされたメモリ ストリームよりもわずかに大きなサイズを保持できます。元のメモリ ストリームの最後に追加のバイトを付加できるからです。メモリ ストリームを厳密に受け取るには、オブジェクトがメモリ ストリームを受け取る方法を判断するためのサイズをプレフィックスとしてオブジェクトに追加するか、メモリ ストリームとそのサイズの文字列値を含む DataObject をクリップボードにコピーします。
メモリをバッキング ストアとして使用してデータを読み書きする方法の例を次に示します。
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
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); } } }
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(); } }
import System.*; import System.IO.*; import System.Text.*; class MemStream { public static void main(String[] args) { int count; ubyte byteArray[]; char charArray[]; UnicodeEncoding uniEncoding = new UnicodeEncoding(); // Create the data to write to the stream. ubyte firstString[] = uniEncoding.GetBytes( "Invalid file path characters are: "); ubyte secondString[] = 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( "Capacity = {0}, Length = {1}, Position = {2}\n", (new Integer( memStream.get_Capacity())).ToString() , (new Long ( memStream.get_Length())).ToString() , (new Long ( memStream.get_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 ubyte[(int)memStream.get_Length()]; count = memStream.Read(byteArray, 0, 20); // Read the remaining bytes, byte by byte. while ((count < memStream.get_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); } finally { memStream.Dispose(); } }//main } //MemStream
スレッド セーフ
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
- MemoryStream クラスのページへのリンク