示例#1
0
        private unsafe void WriteSpan(ReadOnlySpan <byte> source)
        {
            if (!CanWrite)
            {
                ThrowHelper.ThrowNotSupportedException_UnwritableStream();
            }

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

            int r = RandomAccess.WriteAtOffset(_fileHandle, source, _filePosition, _path);

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

            UpdateLengthOnChangePosition();
        }
示例#2
0
        public static void WriteAllBytes(string path, byte[] bytes)
        {
            if (path == null)
            {
                throw new ArgumentNullException(nameof(path), SR.ArgumentNull_Path);
            }
            if (path.Length == 0)
            {
                throw new ArgumentException(SR.Argument_EmptyPath, nameof(path));
            }
            if (bytes == null)
            {
                throw new ArgumentNullException(nameof(bytes));
            }

#if MS_IO_REDIST
            using FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);
            fs.Write(bytes, 0, bytes.Length);
#else
            using SafeFileHandle sfh = OpenHandle(path, FileMode.Create, FileAccess.Write, FileShare.Read);
            RandomAccess.WriteAtOffset(sfh, bytes, 0);
#endif
        }
示例#3
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);
                }
            }