コード例 #1
0
        public override void SetContent(Stream source)
        {
            if (source is null)
            {
                ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
            }

            if (_fileStream is object)
            {
                Delete();
            }

            _fileStream = TempFile();
            int written = 0;
            var bytes   = ArrayPool <byte> .Shared.Rent(c_defaultCopyBufferSize);

            try
            {
                while (true)
                {
                    int read = source.Read(bytes, 0, bytes.Length);
                    if (read <= 0)
                    {
                        break;
                    }

                    written += read;
                    CheckSize(written, MaxSize);
                    _fileStream.Write(bytes, 0, read);
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(bytes);
            }
            _fileStream.Flush();
            // Reset the position to start for reads
            _fileStream.Position -= written;

            Size = written;
            if (DefinedSize > 0 && DefinedSize < Size)
            {
                try
                {
                    Delete(_fileStream);
                }
                catch (Exception error)
                {
                    if (Logger.WarnEnabled)
                    {
                        Logger.FailedToDelete(_fileStream, error);
                    }
                }
                _fileStream = null;
                ThrowHelper.ThrowIOException_OutOfSize(Size, DefinedSize);
            }
            //isRenamed = true;
            SetCompleted();
        }