UnmanagedMemoryStream.Read 方法

定義

多載

Read(Span<Byte>)

將此非受控記憶體資料流中所有位元組讀入指定的位元組範圍。

Read(Byte[], Int32, Int32)

將指定的位元組數讀入指定的陣列。

Read(Span<Byte>)

將此非受控記憶體資料流中所有位元組讀入指定的位元組範圍。

public:
 override int Read(Span<System::Byte> destination);
public:
 override int Read(Span<System::Byte> buffer);
public override int Read (Span<byte> destination);
public override int Read (Span<byte> buffer);
override this.Read : Span<byte> -> int
override this.Read : Span<byte> -> int
Public Overrides Function Read (destination As Span(Of Byte)) As Integer
Public Overrides Function Read (buffer As Span(Of Byte)) As Integer

參數

destinationbuffer
Span<Byte>

當這個方法傳回時,此範圍會包含非受控記憶體資料流中的所有位元組。

傳回

讀入目的地的位元組總數。

適用於

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)。

例外狀況

資料流已關閉。

基礎記憶體不支援讀取。

-或-

CanRead 屬性設定為 false

buffer 參數設定為 null

offset 參數小於零。

-或-

count 參數小於零。

緩衝區陣列的長度減去 offset 參數,小於 count 參數。

範例

下列程式碼範例示範如何使用 類別讀取和寫入 Unmanaged 記憶體 UnmanagedMemoryStream 。 Unmanaged 記憶體區塊會使用 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 時沒有資料流程提供任何資料,方法將會封鎖,直到可以傳回至少一個位元組的資料為止。 即使尚未到達資料流程結尾,實作仍可傳回比要求的位元組少。

適用於