UnmanagedMemoryStream.Read UnmanagedMemoryStream.Read UnmanagedMemoryStream.Read UnmanagedMemoryStream.Read Method

定義

多載

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

將此非受控記憶體資料流中所有位元組讀入指定的位元組範圍。Reads all the bytes of this unmanaged memory stream into the specified span of bytes.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

將指定的位元組數讀入指定的陣列。Reads the specified number of bytes into the specified array.

Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>) Read(Span<Byte>)

將此非受控記憶體資料流中所有位元組讀入指定的位元組範圍。Reads all the bytes of this unmanaged memory stream into the specified span of bytes.

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

參數

destination
Span<Byte>

當這個方法傳回時,此範圍會包含非受控記憶體資料流中的所有位元組。When this method returns, this span contains all the bytes from the unmanaged memory stream.

傳回

讀入目的地的位元組總數。The total number of bytes read into the destination.

Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32) Read(Byte[], Int32, Int32)

將指定的位元組數讀入指定的陣列。Reads the specified number of bytes into the specified array.

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) 之間的值,已由讀取自目前來源的位元組所取代。When this method returns, contains the specified byte array with the values between offset and (offset + count - 1) replaced by the bytes read from the current source. 這個參數會以未初始化的狀態傳遞。This parameter is passed uninitialized.

offset
Int32 Int32 Int32 Int32

buffer 中以零起始的位元組位移,即開始儲存讀取自目前資料流之資料的位置。The zero-based byte offset in buffer at which to begin storing the data read from the current stream.

count
Int32 Int32 Int32 Int32

自目前資料流讀取的最大位元組數。The maximum number of bytes to read from the current stream.

傳回

緩衝區所讀取的總位元組數。The total number of bytes read into the buffer. 如果目前無法取得足夠的位元組,則這個數目可能小於所要求的位元組數,如果已經到達資料流末端,則為零 (0)。This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.

例外狀況

基礎記憶體不支援讀取。The underlying memory does not support reading.

-或--or- CanRead 屬性設定為 falseThe CanRead property is set to false.

buffer 參數設定為 nullThe buffer parameter is set to null.

offset 參數小於零。The offset parameter is less than zero.

-或--or- count 參數小於零。The count parameter is less than zero.

緩衝區陣列的長度減去 offset 參數,小於 count 參數。The length of the buffer array minus the offset parameter is less than the count parameter.

範例

下列程式碼範例示範如何使用UnmanagedMemoryStream類別來讀取和寫入非受控記憶體。The following code example demonstrates how to read from and write to unmanaged memory using the UnmanagedMemoryStream class. 使用Marshal類別配置和解除配置非受控記憶體的區塊。A block of unmanaged memory is allocated and de-allocated using the Marshal class.


// 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();
    }
}

備註

參數會提供array參數 (緩衝區索引) 中要count開始讀取的位元組位移, 而參數會提供要從此資料流程讀取的最大位元組數目。 offsetThe offset parameter gives the offset of the byte in the array parameter (the buffer index) at which to begin reading, and the count parameter gives the maximum number of bytes to be read from this stream. 傳回的值是讀取的實際位元組數目, 如果已到達資料流程末端, 則為零。The returned value is the actual number of bytes read, or zero if the end of the stream is reached. 如果讀取作業成功, 資料流程的目前位置會以讀取的位元組數目來前移。If the read operation is successful, the current position of the stream is advanced by the number of bytes read. 如果發生例外狀況, 資料流程的目前位置會保持不變。If an exception occurs, the current position of the stream is unchanged.

只有Read在到達資料流程結尾之後, 方法才會傳回零。The Read method returns zero only after reaching the end of the stream. 否則, Read在傳回之前, 一律至少從資料流程讀取一個位元組。Otherwise, Read always reads at least one byte from the stream before returning. 如果在呼叫Read時, 資料流程中沒有可用的資料, 此方法將會封鎖, 直到至少有一個位元組的資料可以傳回為止。If no data is available from the stream upon a call to Read, the method will block until at least one byte of data can be returned. 即使尚未到達資料流程的結尾, 執行也可以自由地傳回比要求還要少的位元組。An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached.

適用於