UnmanagedMemoryStream.Read 方法

定义

重载

名称 说明
Read(Span<Byte>)

将此非托管内存流的所有字节读入指定的字节范围。

Read(Byte[], Int32, Int32)

将指定的字节数读入指定的数组。

Read(Span<Byte>)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs

将此非托管内存流的所有字节读入指定的字节范围。

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

参数

destinationbuffer
Span<Byte>

此方法返回时,此范围包含非托管内存流中的所有字节。

返回

读取到目标的字节总数。

适用于

Read(Byte[], Int32, Int32)

Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs
Source:
UnmanagedMemoryStream.cs

将指定的字节数读入指定的数组。

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 参数。

示例

下面的代码示例演示如何使用 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时流中没有可用数据,该方法将阻止,直到可以返回至少一个字节的数据。 即使尚未到达流的末尾,实现也能够自由返回比请求的字节少。

适用于