Stream.ReadAtLeastAsync 方法

定义

异步读取当前流的最小字节数,按读取的字节数推进流中的位置,并监视取消请求。

public System.Threading.Tasks.ValueTask<int> ReadAtLeastAsync(Memory<byte> buffer, int minimumBytes, bool throwOnEndOfStream = true, System.Threading.CancellationToken cancellationToken = default);
member this.ReadAtLeastAsync : Memory<byte> * int * bool * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Function ReadAtLeastAsync (buffer As Memory(Of Byte), minimumBytes As Integer, Optional throwOnEndOfStream As Boolean = true, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

参数

buffer
Memory<Byte>

要将数据写入到的内存区域。

minimumBytes
Int32

要读入缓冲区的最小字节数。

throwOnEndOfStream
Boolean

true 如果在读取 minimumBytes 字节之前到达流的末尾,则引发异常; false 返回的 minimumBytes 小于到达流的末尾的时间。 默认值为 true

cancellationToken
CancellationToken

用于监视取消请求的令牌。

返回

表示异步读取操作的任务。 其 Result 属性的值包含读取到缓冲区中的字节总数。 这可以保证大于或等于minimumBytes何时throwOnEndOfStreamtrue。 这小于 minimumBytes 到达流的末尾且 throwOnEndOfStreamfalse达到时。 如果缓冲区中分配的字节数当前不可用,这可以小于该数目的字节数。

例外

minimumBytes 为负数,或大于长度 buffer

throwOnEndOfStream 是在 true 读取 minimumBytes 数据字节之前到达流的末尾。

取消令牌已取消。 此异常存储在返回的任务中。

注解

如果 minimumBytes 为 0(零),则此读取操作将完成,而无需等待流中的可用数据。

适用于