public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            AssertBufferArguments(buffer, offset, count);

            ValueTask <int> readResult = ReadAsync(new Memory <byte>(buffer, offset, count), cancellationToken);

            return(readResult.IsCompletedSuccessfully
                ? _lastSyncCompletedReadTask.GetTask(readResult.Result)
                : readResult.AsTask());
        }
Beispiel #2
0
        /// <summary>
        /// Reads bytes from stream and puts them into the buffer
        /// </summary>
        /// <param name="buffer">Buffer to read the bytes to.</param>
        /// <param name="offset">Starting index in the buffer.</param>
        /// <param name="count">Maximum number of bytes to read.</param>
        /// <param name="cancellationToken">Token that can be used to cancel this operation.</param>
        /// <returns>Task that can be used to access the number of bytes actually read.</returns>
        public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            ValidateBufferArguments(buffer, offset, count);

            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <int>(cancellationToken));
            }

            try
            {
                int n = Read(buffer, offset, count);
                return(_lastReadTask.GetTask(n));
            }
            catch (Exception ex)
            {
                Debug.Assert(ex is not OperationCanceledException);
                return(Task.FromException <int>(ex));
            }
        }
Beispiel #3
0
        public override Task <int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            ValidateBufferArguments(buffer, offset, count);

            // If cancellation was requested, bail early
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled <int>(cancellationToken));
            }

            try
            {
                int n = Read(buffer, offset, count);
                return(_lastReadTask.GetTask(n));
            }
            catch (OperationCanceledException oce)
            {
                return(Task.FromCanceled <int>(oce));
            }
            catch (Exception exception)
            {
                return(Task.FromException <int>(exception));
            }
        }