internal static IAsyncOperationWithProgress<UInt32, UInt32> WriteAsync_AbstractStream(Stream stream, IBuffer buffer)
{
Debug.Assert(stream != null);
Debug.Assert(stream.CanWrite);
Debug.Assert(buffer != null);
Contract.EndContractBlock();
// Choose the optimal writing strategy for the kind of buffer supplied:
Func<CancellationToken, IProgress<UInt32>, Task<UInt32>> writeOperation;
Byte[] data;
Int32 offset;
// If buffer is backed by a managed array:
if (buffer.TryGetUnderlyingData(out data, out offset))
{
writeOperation = async (cancelToken, progressListener) =>
{
if (cancelToken.IsCancellationRequested) // CancellationToken is non-nullable
return 0;
Debug.Assert(buffer.Length <= Int32.MaxValue);
Int32 bytesToWrite = (Int32)buffer.Length;
await stream.WriteAsync(data, offset, bytesToWrite, cancelToken).ConfigureAwait(continueOnCapturedContext: false);
if (progressListener != null)
progressListener.Report((UInt32)bytesToWrite);
return (UInt32)bytesToWrite;
};
// Otherwise buffer is of an unknown implementation:
}
else
{
writeOperation = async (cancelToken, progressListener) =>
{
if (cancelToken.IsCancellationRequested) // CancellationToken is non-nullable
return 0;
UInt32 bytesToWrite = buffer.Length;
Stream dataStream = buffer.AsStream();
Int32 buffSize = 0x4000;
if (bytesToWrite < buffSize)
buffSize = (Int32)bytesToWrite;
await dataStream.CopyToAsync(stream, buffSize, cancelToken).ConfigureAwait(continueOnCapturedContext: false);
if (progressListener != null)
progressListener.Report((UInt32)bytesToWrite);
return (UInt32)bytesToWrite;
};
} // if-else
// Construct and run the async operation:
return AsyncInfo.Run<UInt32, UInt32>(writeOperation);
} // WriteAsync_AbstractStream