public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
 {
     StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);
     return(cancellationToken.IsCancellationRequested ?
            Task.FromCanceled <int>(cancellationToken) :
            CopyToAsyncCore(destination, bufferSize, cancellationToken));
 }
Example #2
0
        public override void CopyTo(Stream destination, int bufferSize)
        {
            // Since we did not originally override this method, validate the arguments
            // the same way Stream does for back-compat.
            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                base.CopyTo(destination, bufferSize);
                return;
            }

            int originalPosition = _position;

            // Seek to the end of the MemoryStream.
            int remaining = InternalEmulateRead(_length - originalPosition);

            // If we were already at or past the end, there's no copying to do so just quit.
            if (remaining > 0)
            {
                // Call Write() on the other Stream, using our internal buffer and avoiding any
                // intermediary allocations.
                destination.Write(_buffer, originalPosition, remaining);
            }
        }
Example #3
0
        public override Task CopyToAsync(Func <ReadOnlyMemory <byte>, object?, CancellationToken, ValueTask> callback, object?state, int bufferSize, CancellationToken cancellationToken)
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to ReadAsync() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into ReadAsync) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                return(base.CopyToAsync(callback, state, bufferSize, cancellationToken));
            }

            StreamHelpers.ValidateCopyToArgs(this, callback, bufferSize);

            // If canceled - return fast:
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled(cancellationToken));
            }

            // Avoid copying data from this buffer into a temp buffer
            ReadOnlyMemory <byte> memory = new ReadOnlyMemory <byte>(_buffer, _position, _length - _position);

            _position = _length;

            return(callback(memory, state, cancellationToken).AsTask());
        }
        private void CopyTo(Stream destination)
        {
            int bufferSize = GetCopyBufferSize(source);

            StreamHelpers.ValidateCopyToArgs(source, destination, bufferSize);
            CopyToInternal(destination, bufferSize);
        }
        private Task CopyToAsync(Stream destination, CancellationToken cancellationToken)
        {
            int bufferSize = GetCopyBufferSize(source);

            StreamHelpers.ValidateCopyToArgs(source, destination, bufferSize);

            return(CopyToAsyncInternal(destination, bufferSize, cancellationToken));
        }
Example #6
0
            public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
            {
                // Validate arguments for compat, since previously this
                // method was inherited from Stream, which did check its arguments.
                StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

                return(cancellationToken.IsCancellationRequested ?
                       Task.FromCanceled(cancellationToken) :
                       Task.CompletedTask);
            }
Example #7
0
        public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
        {
            // This implementation offers better performance compared to the base class version.

            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to ReadAsync() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into ReadAsync) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                return(base.CopyToAsync(destination, bufferSize, cancellationToken));
            }

            // If cancelled - return fast:
            if (cancellationToken.IsCancellationRequested)
            {
                return(Task.FromCanceled(cancellationToken));
            }

            // Avoid copying data from this buffer into a temp buffer:
            //   (require that InternalEmulateRead does not throw,
            //    otherwise it needs to be wrapped into try-catch-Task.FromException like memStrDest.Write below)

            int pos = _position;
            int n   = InternalEmulateRead(_length - _position);

            // If we were already at or past the end, there's no copying to do so just quit.
            if (n == 0)
            {
                return(Task.CompletedTask);
            }

            // If destination is not a memory stream, write there asynchronously:
            MemoryStream memStrDest = destination as MemoryStream;

            if (memStrDest == null)
            {
                return(destination.WriteAsync(_buffer, pos, n, cancellationToken));
            }

            try
            {
                // If destination is a MemoryStream, CopyTo synchronously:
                memStrDest.Write(_buffer, pos, n);
                return(Task.CompletedTask);
            }
            catch (Exception ex)
            {
                return(Task.FromException(ex));
            }
        }
Example #8
0
        public virtual void CopyTo(Stream destination, int bufferSize)
        {
            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            byte[] buffer = new byte[bufferSize];
            int    read;

            while ((read = Read(buffer, 0, buffer.Length)) != 0)
            {
                destination.Write(buffer, 0, read);
            }
        }
Example #9
0
        public override Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
        {
            // This implementation offers better performance compared to the base class version.

            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to ReadAsync() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into ReadAsync) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                return(base.CopyToAsync(destination, bufferSize, cancellationToken));
            }

            return(CopyToAsyncImpl(destination, bufferSize, cancellationToken));
        }
Example #10
0
        public virtual void CopyTo(Stream destination, int bufferSize)
        {
            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            byte[] buffer = ArrayPool <byte> .Shared.Rent(bufferSize);

            try
            {
                int read;
                while ((read = Read(buffer, 0, buffer.Length)) != 0)
                {
                    destination.Write(buffer, 0, read);
                }
            }
            finally
            {
                ArrayPool <byte> .Shared.Return(buffer);
            }
        }
Example #11
0
        public override void CopyTo(ReadOnlySpanAction <byte, object?> callback, object?state, int bufferSize)
        {
            // If we have been inherited into a subclass, the following implementation could be incorrect
            // since it does not call through to Read() which a subclass might have overridden.
            // To be safe we will only use this implementation in cases where we know it is safe to do so,
            // and delegate to our base class (which will call into Read) when we are not sure.
            if (GetType() != typeof(MemoryStream))
            {
                base.CopyTo(callback, state, bufferSize);
                return;
            }

            StreamHelpers.ValidateCopyToArgs(this, callback, bufferSize);

            // Retrieve a span until the end of the MemoryStream.
            ReadOnlySpan <byte> span = new ReadOnlySpan <byte>(_buffer, _position, _length - _position);

            _position = _length;

            // Invoke the callback, using our internal span and avoiding any
            // intermediary allocations.
            callback(span, state);
        }
Example #12
0
            public override void CopyTo(Stream destination, int bufferSize)
            {
                StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

                // After we validate arguments this is a nop.
            }
Example #13
0
        public virtual Task CopyToAsync(Stream destination, int bufferSize, CancellationToken cancellationToken)
        {
            StreamHelpers.ValidateCopyToArgs(this, destination, bufferSize);

            return(CopyToAsyncInternal(destination, bufferSize, cancellationToken));
        }