MemoryStream.Read 方法

定义

重载

名称 说明
Read(Span<Byte>)

从当前内存流中读取字节序列,并通过读取的字节数推进内存流中的位置。

Read(Byte[], Int32, Int32)

从当前流中读取字节块并将数据写入缓冲区。

Read(Span<Byte>)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.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>

内存区域。 此方法返回时,此范围的内容将替换为从当前内存流源读取的字节。

返回

读取到缓冲区中的字节总数。 如果当前没有可用字节数,则缓冲区中分配的字节数可能小于零(0),如果已达到内存流的末尾,则为零(0)。

适用于

Read(Byte[], Int32, Int32)

Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.cs
Source:
MemoryStream.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

要读取的最大字节数。

返回

写入缓冲区的总字节数。 如果该字节数当前不可用,则这可以小于请求的字节数;如果在读取任何字节之前到达流的末尾,则为零。

例外

buffernull

offsetcount 为负数。

offset 从缓冲区长度中减去的长度小于 count

当前流实例已关闭。

示例

该代码示例是 MemoryStream 类中的一个较大示例的一部分。

// Read the first 20 bytes from the stream.
byteArray = new byte[memStream.Length];
count = memStream.Read(byteArray, 0, 20);
' Read the first 20 bytes from the stream.
byteArray = _
    New Byte(CType(memStream.Length, Integer)){}
count = memStream.Read(byteArray, 0, 20)

注解

此方法重写 Read

offset 参数提供从当前流写入到的第一个字节 buffer 的偏移量。 该 count 参数提供从当前流中读取的最大字节数。 返回的值是读取的实际字节数,如果到达流的末尾,则返回的字节数为零。

如果读取操作成功,则流中的当前位置会按读取的字节数向前推进。 如果发生异常,流中的当前位置保持不变。

Read仅当到达流的末尾时,该方法才会返回零。 在所有其他情况下,在返回之前, Read 始终从流中读取至少一个字节。 根据定义,如果在调用 Read时流中没有可用数据,该方法 Read 将返回零(自动到达流的末尾)。 即使尚未到达流的末尾,实现也能够自由返回比请求的字节少。

用于 BinaryReader 读取基元数据类型。

Caution

如果参数中指定的 buffer 字节数组是该方法返回 GetBuffer 的基础缓冲区,则覆盖数组内容,并且不会引发异常。

另请参阅

适用于