Beispiel #1
0
        public override Task FlushAsync(CancellationToken cancellationToken) => Task.CompletedTask; // no buffering = nothing to flush

        private unsafe int ReadSpan(Span <byte> destination)
        {
            if (!CanRead)
            {
                ThrowHelper.ThrowNotSupportedException_UnreadableStream();
            }

            Debug.Assert(!_fileHandle.IsClosed, "!_handle.IsClosed");

            int r = RandomAccess.ReadAtOffset(_fileHandle, destination, _filePosition, _path);

            Debug.Assert(r >= 0, $"RandomAccess.ReadAtOffset returned {r}.");
            _filePosition += r;

            return(r);
        }
Beispiel #2
0
            private void ExecuteInternal()
            {
                Debug.Assert(_operation >= Operation.Read && _operation <= Operation.WriteGather);

                long      result    = 0;
                Exception?exception = null;

                try
                {
                    // This is the operation's last chance to be canceled.
                    if (_cancellationToken.IsCancellationRequested)
                    {
                        exception = new OperationCanceledException(_cancellationToken);
                    }
                    else
                    {
                        switch (_operation)
                        {
                        case Operation.Read:
                            Memory <byte> writableSingleSegment = MemoryMarshal.AsMemory(_singleSegment);
                            result = RandomAccess.ReadAtOffset(_fileHandle, writableSingleSegment.Span, _fileOffset);
                            break;

                        case Operation.Write:
                            RandomAccess.WriteAtOffset(_fileHandle, _singleSegment.Span, _fileOffset);
                            break;

                        case Operation.ReadScatter:
                            Debug.Assert(_readScatterBuffers != null);
                            result = RandomAccess.ReadScatterAtOffset(_fileHandle, _readScatterBuffers, _fileOffset);
                            break;

                        case Operation.WriteGather:
                            Debug.Assert(_writeGatherBuffers != null);
                            RandomAccess.WriteGatherAtOffset(_fileHandle, _writeGatherBuffers, _fileOffset);
                            break;
                        }
                    }
                }
                catch (Exception e)
                {
                    exception = e;
                }
                finally
                {
                    _operation          = Operation.None;
                    _context            = null;
                    _cancellationToken  = default;
                    _singleSegment      = default;
                    _readScatterBuffers = null;
                    _writeGatherBuffers = null;
                }

                if (exception == null)
                {
                    _source.SetResult(result);
                }
                else
                {
                    _source.SetException(exception);
                }
            }