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);
}
}