lib/Task-Parallel-Library/StreamExtensions.cs
/// <summary>
/// Asynchronously writes a sequence of bytes to a stream, advances the position within the stream by the number of bytes written,
/// and monitors cancellation requests.
/// </summary>
/// <remarks>
/// Use the <see cref="Stream.CanWrite"/> property to determine whether the stream instance supports writing.
/// <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>
/// </remarks>
/// <param name="stream">The stream to write data to.</param>
/// <param name="buffer">The buffer to read the data from.</param>
/// <param name="offset">The zero-based byte offset in buffer from which to begin copying bytes to the stream.</param>
/// <param name="count">The maximum number of bytes to write.</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 write operation.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <para>If <paramref name="stream"/> is <see langword="null"/>.</para>
/// <para>-or-</para>
/// <para>If <paramref name="buffer"/> is <see langword="null"/>.</para>
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <para>If <paramref name="offset"/> is negative.</para>
/// <para>-or-</para>
/// <para>If <paramref name="count"/> is negative.</para>
/// </exception>
/// <exception cref="ArgumentException">
/// If the sum of <paramref name="offset"/> and <paramref name="count"/> is larger than the buffer length.
/// </exception>
/// <exception cref="NotSupportedException">If <paramref name="stream"/> does not support writing.</exception>
/// <exception cref="ObjectDisposedException">If <paramref name="stream"/> has been disposed.</exception>
/// <exception cref="InvalidOperationException">If <paramref name="stream"/> is currently in use by a previous write operation.</exception>
Asynchronously writes a sequence of bytes to a stream, advances the position within the stream by the number of bytes written,
and monitors cancellation requests.
Asynchronously writes a sequence of bytes to a stream advances the position within the stream by the number of bytes written and monitors cancellation requests .
public static Task WriteAsync(this Stream stream, byte[] buffer, int offset, int count, CancellationToken cancellationToken)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");
            if (cancellationToken.IsCancellationRequested)
                return GetCanceledTask<bool>();
            #if NET45PLUS
            return stream.WriteAsync(buffer, offset, count, cancellationToken);
            #else
            return Task.Factory.FromAsync(stream.BeginWrite, stream.EndWrite, buffer, offset, count, null);
            #endif
        }
public static Task WriteAsync ( this Stream stream , byte [ ] buffer , int offset , int count , CancellationToken cancellationToken ) { if ( stream == null ) throw new ArgumentNullException ( " " ) ; if ( cancellationToken . IsCancellationRequested ) return GetCanceledTask < bool > ( ) ; if NET45PLUS return stream . WriteAsync ( buffer , offset , count , cancellationToken ) ; else return Task . Factory . FromAsync ( stream . BeginWrite , stream . EndWrite , buffer , offset , count , null ) ; endif }
Asynchronously writes a sequence of bytes to a stream, advances the position within the stream by the number of bytes written,
and monitors cancellation requests.
Asynchronously writes a sequence of bytes to a stream advances the position within the stream by the number of bytes written and monitors cancellation requests .
// This code requires the `Stream` class provide an implementation of `WriteAsync`. The unit tests will // detect any case where this results in a stack overflow.
A task that represents the asynchronous write operation.
A task that represents the asynchronous write operation .
If  is .-or-If  is .
If is . - or - If is .
If  is negative.-or-If  is negative.
If is negative . - or - If is negative .
If the sum of  and  is larger than the buffer length.
If the sum of and is larger than the buffer length .
If  does not support writing.
If does not support writing .
If  has been disposed.
If has been disposed .
If  is currently in use by a previous write operation.
If is currently in use by a previous write operation .
Stream
this
buffer
byte[]
The buffer to read the data from.
The buffer to read the data from .
offset
int
The zero-based byte offset in buffer from which to begin copying bytes to the stream.
The zero - based byte offset in buffer from which to begin copying bytes to the stream .
count
int
The maximum number of bytes to write.
The maximum number of bytes to write .
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 stream to write data to.
The stream to write data to .
remarks
Use the  property to determine whether the stream instance supports writing.

If the operation is canceled before it completes, the returned task contains the
value for the  property.
Use the property to determine whether the stream instance supports writing . If the operation is canceled before it completes the returned task contains the value for the property .