UnmanagedMemoryStream.Read 方法   
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
| Read(Span<Byte>) | 将此非管理的内存流的所有字节读入指定的字节跨度。 | 
| Read(Byte[], Int32, Int32) | 将指定数量的字节读入指定的数组。 | 
Read(Span<Byte>)
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
- Source:
- UnmanagedMemoryStream.cs
将此非管理的内存流的所有字节读入指定的字节跨度。
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> -> intoverride this.Read : Span<byte> -> intPublic Overrides Function Read (destination As Span(Of Byte)) As IntegerPublic Overrides Function Read (buffer As Span(Of Byte)) As Integer参数
返回
读入目标的总字节数。
适用于
Read(Byte[], Int32, Int32)
- 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 -> intPublic 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时流中没有数据可用,则方法将阻塞,直到至少可以返回一个字节的数据。 实现可以自由返回比请求的字节少,即使尚未到达流的末尾。