From b4b2dd6bf22c9e234022d8675e0e7a34c373cbb1 Mon Sep 17 00:00:00 2001 From: Pavel Krymets Date: Thu, 21 Jul 2016 16:40:48 -0700 Subject: [PATCH] Add buffer size parameter to CopyTo method (#674) --- .../StreamCopyOperation.cs | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs b/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs index ef81ac3baf..12067fef65 100644 --- a/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs +++ b/src/Microsoft.AspNetCore.Http.Extensions/StreamCopyOperation.cs @@ -15,11 +15,29 @@ namespace Microsoft.AspNetCore.Http.Extensions { private const int DefaultBufferSize = 4096; - public static async Task CopyToAsync(Stream source, Stream destination, long? count, CancellationToken cancel) + /// Asynchronously reads the bytes from the source stream and writes them to another stream. + /// A task that represents the asynchronous copy operation. + /// The stream from which the contents will be copied. + /// The stream to which the contents of the current stream will be copied. + /// The count of bytes to be copied. + /// The token to monitor for cancellation requests. The default value is . + public static Task CopyToAsync(Stream source, Stream destination, long? count, CancellationToken cancel) + { + return CopyToAsync(source, destination, count, DefaultBufferSize, cancel); + } + + /// Asynchronously reads the bytes from the source stream and writes them to another stream, using a specified buffer size. + /// A task that represents the asynchronous copy operation. + /// The stream from which the contents will be copied. + /// The stream to which the contents of the current stream will be copied. + /// The count of bytes to be copied. + /// The size, in bytes, of the buffer. This value must be greater than zero. The default size is 4096. + /// The token to monitor for cancellation requests. The default value is . + public static async Task CopyToAsync(Stream source, Stream destination, long? count, int bufferSize, CancellationToken cancel) { long? bytesRemaining = count; - var buffer = ArrayPool.Shared.Rent(DefaultBufferSize); + var buffer = ArrayPool.Shared.Rent(bufferSize); try { Debug.Assert(source != null);