UnmanagedMemoryStream.Read メソッド
アセンブリ: mscorlib (mscorlib.dll 内)

Public Overrides Function Read ( _ <InAttribute> <OutAttribute> buffer As Byte(), _ offset As Integer, _ count As Integer _ ) As Integer
Dim instance As UnmanagedMemoryStream Dim buffer As Byte() Dim offset As Integer Dim count As Integer Dim returnValue As Integer returnValue = instance.Read(buffer, offset, count)
public: virtual int Read ( [InAttribute] [OutAttribute] array<unsigned char>^ buffer, int offset, int count ) override
public int Read ( /** @attribute InAttribute() */ /** @attribute OutAttribute() */ byte[] buffer, int offset, int count )
- buffer
このメソッドが返されるときに、指定したバイト配列の offset から (offset + count - 1) までの値が、現在のソースから読み取られたバイトに置き換えられます。このパラメータは初期化せずに渡されます。
バッファに読み取られた合計バイト数。要求しただけのバイト数を読み取ることができなかった場合、この値は要求したバイト数より小さくなります。ストリームの末尾に到達した場合は 0 (ゼロ) になることがあります。


offset パラメータは、読み取りを開始する位置の array パラメータのバイトのオフセット (バッファ インデックス) を指定し、count パラメータは、このストリームから読み取る最大バイト数を指定します。戻り値は、実際に読み取ったバイト数であり、ストリームの末尾に到達している場合は 0 です。読み取り操作が正常に終了した場合、ストリームの現在位置は読み取ったバイト数だけ進みます。例外が発生した場合、ストリームの現在位置は変更されません。
Read メソッドは、ストリームの末尾に到達した後に必ず 0 を返します。それ以外の場合、Read は戻り値を返す前に常にストリームから少なくとも 1 バイトを読み取ります。Read の呼び出し時に利用できるデータがストリームにない場合、メソッドは少なくとも 1 バイトのデータを返すまでブロックします。メソッドの実装は、ストリームの末尾に到達していない場合でも、要求された数に満たないバイトを返すようにすることができます。

UnmanagedMemoryStream クラスを使用して、アンマネージ メモリから読み取る方法と、アンマネージ メモリに書き込む方法を次のコード例に示します。アンマネージ メモリのブロックは、Marshal クラスを使用して割り当ておよび割り当て解除されます。
// Note: you must compile this sample using the unsafe flag. // From the command line, type the following: csc sample.cs /unsafe using System; using System.IO; using System.Text; using System.Runtime.InteropServices; unsafe class TestWriter { static void Main() { // Create som data to read and write. byte[] message = UnicodeEncoding.Unicode.GetBytes("Here is some data."); // Allocate a block of unmanaged memory and return an IntPtr object. IntPtr memIntPtr = Marshal.AllocHGlobal(message.Length); // Get a byte pointer from the IntPtr object. byte* memBytePtr = (byte*) memIntPtr.ToPointer(); // Create an UnmanagedMemoryStream object using a pointer to unmanaged memory. UnmanagedMemoryStream writeStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Write); // Write the data. writeStream.Write(message, 0, message.Length); // Close the stream. writeStream.Close(); // Create another UnmanagedMemoryStream object using a pointer to unmanaged memory. UnmanagedMemoryStream readStream = new UnmanagedMemoryStream(memBytePtr, message.Length, message.Length, FileAccess.Read); // Create a byte array to hold data from unmanaged memory. byte[] outMessage = new byte[message.Length]; // Read from unmanaged memory to the byte array. readStream.Read(outMessage, 0, message.Length); // Close the stream. readStream.Close(); // Display the data to the console. Console.WriteLine(UnicodeEncoding.Unicode.GetString(outMessage)); // Free the block of unmanaged memory. Marshal.FreeHGlobal(memIntPtr); Console.ReadLine(); } }

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


Weblioに収録されているすべての辞書からUnmanagedMemoryStream.Read メソッドを検索する場合は、下記のリンクをクリックしてください。

- UnmanagedMemoryStream.Read メソッドのページへのリンク