UnmanagedMemoryStream.Read メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
オーバーロード
| 名前 | 説明 |
|---|---|
| Read(Span<Byte>) |
このアンマネージ メモリ ストリームのすべてのバイトを、指定されたバイトスパンに読み取ります。 |
| Read(Byte[], Int32, Int32) |
指定したバイト数を、指定した配列に読み取ります。 |
Read(Span<Byte>)
このアンマネージ メモリ ストリームのすべてのバイトを、指定されたバイトスパンに読み取ります。
public:
override int Read(Span<System::Byte> buffer);
public:
override int Read(Span<System::Byte> destination);
public override int Read(Span<byte> buffer);
public override int Read(Span<byte> destination);
override this.Read : Span<byte> -> int
override this.Read : Span<byte> -> int
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
パラメーター
返品
宛先に読み取られた合計バイト数。
適用対象
Read(Byte[], Int32, Int32)
指定したバイト数を、指定した配列に読み取ります。
public:
override int Read(cli::array <System::Byte> ^ buffer, int offset, int count);
public override int Read(byte[] buffer, int offset, int count);
override this.Read : byte[] * int * int -> int
Public Overrides Function Read (buffer As Byte(), offset As Integer, count As Integer) As Integer
パラメーター
- buffer
- Byte[]
このメソッドから制御が戻るときに、指定されたバイト配列が格納され、 offset から (offset + count - 1) までの値が現在のソースから読み取られたバイトに置き換えられます。 このパラメーターは初期化せずに渡されます。
- offset
- Int32
現在のストリームから読み取られたデータの格納を開始する buffer の 0 から始まるバイト オフセット。
- count
- Int32
現在のストリームから読み取る最大バイト数。
返品
バッファーに読み込まれるバイトの合計数。 この値は、現在使用できないバイト数の場合は要求されたバイト数より小さく、ストリームの末尾に達した場合は 0 を指定できます。
例外
ストリームが閉じられます。
buffer パラメーターを null に設定します。
バッファー配列の長さから offset パラメーターを引いた値が、 count パラメーターより小さい。
例
次のコード例では、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 some 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();
}
}
注釈
offset パラメーターは、読み取りを開始するarray パラメーター (バッファー インデックス) のバイトのオフセットを指定し、count パラメーターは、このストリームから読み取る最大バイト数を指定します。 返される値は、読み取られた実際のバイト数です。ストリームの末尾に達した場合は 0 です。 読み取り操作が成功した場合、ストリームの現在位置は読み取られたバイト数だけ進みます。 例外が発生した場合、ストリームの現在位置は変更されません。
Read メソッドは、ストリームの末尾に到達した後にのみ 0 を返します。 それ以外の場合、 Read は常にストリームから少なくとも 1 バイトを読み取ってから戻ります。 Readの呼び出し時にストリームからデータが使用できない場合、メソッドは少なくとも 1 バイトのデータが返されるまでブロックします。 実装は、ストリームの末尾に達していない場合でも、要求されたバイト数よりも少ないバイト数を返します。