MemoryStream.ReadAsync 方法

定义

重载

ReadAsync(Memory<Byte>, CancellationToken)

从当前内存流异步读取字节的序列,将该序列写入 destination,按读取的字节数向前移动内存流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into destination, advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.

ReadAsync(Byte[], Int32, Int32, CancellationToken)

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

ReadAsync(Memory<Byte>, CancellationToken)

从当前内存流异步读取字节的序列,将该序列写入 destination,按读取的字节数向前移动内存流中的位置,并监视取消请求。Asynchronously reads a sequence of bytes from the current memory stream, writes the sequence into destination, advances the position within the memory stream by the number of bytes read, and monitors cancellation requests.

public override System.Threading.Tasks.ValueTask<int> ReadAsync (Memory<byte> destination, System.Threading.CancellationToken cancellationToken = default);
override this.ReadAsync : Memory<byte> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<int>
Public Overrides Function ReadAsync (destination As Memory(Of Byte), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Integer)

参数

destination
Memory<Byte>

要将数据写入的内存区域。The region of memory to write the data into.

cancellationToken
CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值是 NoneThe default value is None.

返回

ValueTask<Int32>

表示异步读取操作的任务。A task that represents the asynchronous read operation. Result 属性的值包含读入 destination 的总字节数。The value of its Result property contains the total number of bytes read into the destination. 如果很多字节当前不可用,则结果值可小于在 destination 中分配的字节数;如果已到达内存流结尾,则结果值可以为 0(零)。The result value can be less than the number of bytes allocated in destination if that many bytes are not currently available, or it can be 0 (zero) if the end of the memory stream has been reached.

适用于

ReadAsync(Byte[], Int32, Int32, CancellationToken)

从当前流异步读取字节的序列,将流中的位置提升读取的字节数,并监视取消请求。Asynchronously reads a sequence of bytes from the current stream, advances the position within the stream by the number of bytes read, and monitors cancellation requests.

public:
 override System::Threading::Tasks::Task<int> ^ ReadAsync(cli::array <System::Byte> ^ buffer, int offset, int count, System::Threading::CancellationToken cancellationToken);
public override System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
[System.Runtime.InteropServices.ComVisible(false)]
public override System.Threading.Tasks.Task<int> ReadAsync (byte[] buffer, int offset, int count, System.Threading.CancellationToken cancellationToken);
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
[<System.Runtime.InteropServices.ComVisible(false)>]
override this.ReadAsync : byte[] * int * int * System.Threading.CancellationToken -> System.Threading.Tasks.Task<int>
Public Overrides Function ReadAsync (buffer As Byte(), offset As Integer, count As Integer, cancellationToken As CancellationToken) As Task(Of Integer)

参数

buffer
Byte[]

要写入数据的缓冲区。The buffer to write the data into.

offset
Int32

buffer 中的字节偏移量,从该偏移量开始写入从流中读取的数据。The byte offset in buffer at which to begin writing data from the stream.

count
Int32

最多读取的字节数。The maximum number of bytes to read.

cancellationToken
CancellationToken

要监视取消请求的标记。The token to monitor for cancellation requests. 默认值是 NoneThe default value is None.

返回

Task<Int32>

表示异步读取操作的任务。A task that represents the asynchronous read operation. TResult 参数的值包含读入缓冲区的总字节数。The value of the TResult parameter contains the total number of bytes read into the buffer. 如果当前可用字节数少于所请求的字节数,则该结果值可小于所请求的字节数;如果已到达流结尾时,则为 0(零)。The result value can be less than the number of bytes requested if the number of bytes currently available is less than the requested number, or it can be 0 (zero) if the end of the stream has been reached.

属性

例外

buffernullbuffer is null.

offsetcount 为负数。offset or count is negative.

offsetcount 的总和大于缓冲区长度。The sum of offset and count is larger than the buffer length.

流不支持读取。The stream does not support reading.

已释放流。The stream has been disposed.

之前的读取操作当前正在使用流。The stream is currently in use by a previous read operation.

注解

如果在操作完成前取消该操作,则返回的任务包含 Canceled 属性的值 StatusIf the operation is canceled before it completes, the returned task contains the Canceled value for the Status property.

可以通过创建类的实例 CancellationTokenSource 并将 Token 属性作为参数传递来创建取消标记 cancellationTokenYou can create a cancellation token by creating an instance of the CancellationTokenSource class and passing the Token property as the cancellationToken parameter.

适用于