lib/Task-Parallel-Library/StreamExtensions.cs
/// <summary>
/// Asynchronously reads the bytes from a source stream and writes them to a destination stream,
/// using a specified buffer size and cancellation token.
/// </summary>
/// <remarks>
/// <para>If the operation is canceled before it completes, the returned task contains the <see cref="TaskStatus.Canceled"/>
/// value for the <see cref="Task.Status"/> property.</para>
/// <para>
/// Copying begins at the current position in <paramref name="stream"/>.
/// </para>
/// </remarks>
/// <param name="stream">The source stream.</param>
/// <param name="destination">The stream to which the contents of the source stream will be copied.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.</param>
/// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
/// <returns>A task that represents the asynchronous copy operation.</returns>
/// <exception cref="ArgumentNullException">
/// <para>If <paramref name="stream"/> is <see langword="null"/>.</para>
/// <para>-or-</para>
/// <para>If <paramref name="destination"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// If <paramref name="bufferSize"/> is negative or zero.
/// </exception>
/// <exception cref="ObjectDisposedException">
/// <para>If <paramref name="stream"/> is disposed.</para>
/// <para>-or-</para>
/// <para>If <paramref name="destination"/> is disposed.</para>
/// </exception>
/// <exception cref="NotSupportedException">
/// <para>If <paramref name="stream"/> does not support reading.</para>
/// <para>-or-</para>
/// <para>If <paramref name="destination"/> does not support writing.</para>
/// </exception>
Asynchronously reads the bytes from a source stream and writes them to a destination stream,
using a specified buffer size and cancellation token.
Asynchronously reads the bytes from a source stream and writes them to a destination stream using a specified buffer size and cancellation token .
public static Task CopyToAsync(this Stream stream, Stream destination, int bufferSize, CancellationToken cancellationToken)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (destination == null)
                throw new ArgumentNullException("destination");
            if (!stream.CanRead)
                throw new NotSupportedException("The stream does not support reading");
            if (!destination.CanWrite)
                throw new NotSupportedException("The destination does not support writing");
            if (bufferSize <= 0)
                throw new ArgumentOutOfRangeException("bufferSize");
            if (cancellationToken.IsCancellationRequested)
                return GetCanceledTask<bool>();
            #if NET45PLUS
            return stream.CopyToAsync(destination, bufferSize, cancellationToken);
            #else
            return CopyToAsync(stream, destination, new byte[bufferSize], cancellationToken);
            #endif
        }
public static Task CopyToAsync ( this Stream stream , Stream destination , int bufferSize , CancellationToken cancellationToken ) { if ( stream == null ) throw new ArgumentNullException ( " " ) ; if ( destination == null ) throw new ArgumentNullException ( " " ) ; if ( ! stream . CanRead ) throw new NotSupportedException ( " " ) ; if ( ! destination . CanWrite ) throw new NotSupportedException ( " " ) ; if ( bufferSize <= 0 ) throw new ArgumentOutOfRangeException ( " " ) ; if ( cancellationToken . IsCancellationRequested ) return GetCanceledTask < bool > ( ) ; if NET45PLUS return stream . CopyToAsync ( destination , bufferSize , cancellationToken ) ; else return CopyToAsync ( stream , destination , new byte [ bufferSize ] , cancellationToken ) ; endif }
Asynchronously reads the bytes from a source stream and writes them to a destination stream,
using a specified buffer size and cancellation token.
Asynchronously reads the bytes from a source stream and writes them to a destination stream using a specified buffer size and cancellation token .
// This code requires the `Stream` class provide an implementation of `CopyToAsync`. The unit tests will // detect any case where this results in a stack overflow.
A task that represents the asynchronous copy operation.
A task that represents the asynchronous copy operation .
If  is .-or-If  is .
If is . - or - If is .
If  is negative or zero.
If is negative or zero .
If  is disposed.-or-If  is disposed.
If is disposed . - or - If is disposed .
If  does not support reading.-or-If  does not support writing.
If does not support reading . - or - If does not support writing .
Stream
this
destination
Stream
The stream to which the contents of the source stream will be copied.
The stream to which the contents of the source stream will be copied .
bufferSize
int
The size, in bytes, of the buffer. This value must be greater than zero. The default size is 81920.
The size in bytes of the buffer . This value must be greater than zero . The default size is 81920 .
cancellationToken
CancellationToken
The token to monitor for cancellation requests. The default value is .
The token to monitor for cancellation requests . The default value is .
stream
The source stream.
The source stream .
remarks
If the operation is canceled before it completes, the returned task contains the
value for the  property.
Copying begins at the current position in .
If the operation is canceled before it completes the returned task contains the value for the property . Copying begins at the current position in .