UnmanagedMemoryStream.Read 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
| 名稱 | Description |
|---|---|
| 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 ,從該位元組開始儲存從當前串流讀取的資料。
- 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位元組的偏移量,該參數則表示從該串流中可讀取的最大位元組數。 回傳的值為實際讀取的位元組數,若到達串流末端則為零。 若讀取操作成功,串流當前位置將依讀取位元組數前進。 若發生例外,串流的當前位置保持不變。
Read該方法只有在到達串流結束後才回傳零。 否則, Read 總是至少讀取一個位元組後再返回。 若呼叫 時 Read串流中無資料可用,該方法將阻塞,直到至少回傳一個位元組資料。 即使串流尚未到達終點,實作也可以自由回傳比請求的位元組少。