UnmanagedMemoryStream.Read 方法
定义
重载
| Read(Span<Byte>) |
将此非管理的内存流的所有字节读入指定的字节跨度。Reads all the bytes of this unmanaged memory stream into the specified span of bytes. |
| Read(Byte[], Int32, Int32) |
将指定数量的字节读入指定的数组。Reads the specified number of bytes into the specified array. |
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
参数
此方法返回时,此跨度包含非管理的内存流中的所有字节。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)
将指定数量的字节读入指定的数组。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
buffer 中的从零开始的字节偏移量,从此处开始存储从当前流中读取的数据。The zero-based byte offset in buffer at which to begin storing the data read from the current stream.
- count
- 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 stream is closed.
基础内存不支持读取。The underlying memory does not support reading.
- 或 --or-
将 CanRead 属性设置为 false。The CanRead property is set to false.
buffer 参数设置为 null。The 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();
}
}
注解
offset参数提供参数中的字节偏移量 array (要开始读取的缓冲索引) , count 参数提供要从此流中读取的最大字节数。The 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.