System.Net.Http.StreamToStreamCopy.CopyAsync C# (CSharp) Method

CopyAsync() public static method

Copies the source stream from its current position to the destination stream at its current position.
public static CopyAsync ( Stream source, Stream destination, int bufferSize, bool disposeSource, CancellationToken cancellationToken = default(CancellationToken) ) : Task
source System.IO.Stream The source stream from which to copy.
destination System.IO.Stream The destination stream to which to copy.
bufferSize int The size of the buffer to allocate if one needs to be allocated.
disposeSource bool Whether to dispose of the source stream after the copy has finished successfully.
cancellationToken System.Threading.CancellationToken CancellationToken used to cancel the copy operation.
return Task
        public static Task CopyAsync(Stream source, Stream destination, int bufferSize, bool disposeSource, CancellationToken cancellationToken = default(CancellationToken))
        {
            Debug.Assert(source != null);
            Debug.Assert(destination != null);
            Debug.Assert(bufferSize > 0);

            try
            {
                Task copyTask = source.CopyToAsync(destination, bufferSize, cancellationToken);
                return disposeSource ?
                    DisposeSourceWhenCompleteAsync(copyTask, source) :
                    copyTask;
            }
            catch (Exception e)
            {
                // For compatibility with the previous implementation, catch everything (including arg exceptions) and
                // store errors into the task rather than letting them propagate to the synchronous caller.
                return Task.FromException(e);
            }
        }

Usage Example

コード例 #1
0
        protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
        {
            Debug.Assert(stream != null);

            PrepareContent();
            // If the stream can't be re-read, make sure that it gets disposed once it is consumed.
            return(StreamToStreamCopy.CopyAsync(_content, stream, _bufferSize, !_content.CanSeek));
        }
All Usage Examples Of System.Net.Http.StreamToStreamCopy::CopyAsync