|
2 | 2 | // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | using System;
|
| 5 | +using System.Buffers; |
5 | 6 | using System.Diagnostics;
|
6 | 7 | using System.IO;
|
7 | 8 | using System.Threading;
|
8 | 9 | using System.Threading.Tasks;
|
9 | 10 |
|
10 |
| -namespace Microsoft.AspNet.Http |
| 11 | +namespace Microsoft.AspNet.Http.Extensions |
11 | 12 | {
|
12 | 13 | // FYI: In most cases the source will be a FileStream and the destination will be to the network.
|
13 |
| - internal static class StreamCopyOperation |
| 14 | + public static class StreamCopyOperation |
14 | 15 | {
|
15 |
| - internal static async Task CopyToAsync(Stream source, byte[] buffer, Stream destination, long? length, CancellationToken cancel) |
| 16 | + private const int DefaultBufferSize = 4096; |
| 17 | + |
| 18 | + public static async Task CopyToAsync(Stream source, Stream destination, long? length, CancellationToken cancel) |
16 | 19 | {
|
17 | 20 | long? bytesRemaining = length;
|
18 |
| - Debug.Assert(source != null); |
19 |
| - Debug.Assert(destination != null); |
20 |
| - Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0); |
21 |
| - Debug.Assert(buffer != null); |
22 | 21 |
|
23 |
| - while (true) |
| 22 | + var buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize); |
| 23 | + try |
24 | 24 | {
|
25 |
| - // The natural end of the range. |
26 |
| - if (bytesRemaining.HasValue && bytesRemaining.Value <= 0) |
| 25 | + Debug.Assert(source != null); |
| 26 | + Debug.Assert(destination != null); |
| 27 | + Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0); |
| 28 | + Debug.Assert(buffer != null); |
| 29 | + |
| 30 | + while (true) |
27 | 31 | {
|
28 |
| - return; |
29 |
| - } |
| 32 | + // The natural end of the range. |
| 33 | + if (bytesRemaining.HasValue && bytesRemaining.Value <= 0) |
| 34 | + { |
| 35 | + return; |
| 36 | + } |
30 | 37 |
|
31 |
| - cancel.ThrowIfCancellationRequested(); |
| 38 | + cancel.ThrowIfCancellationRequested(); |
32 | 39 |
|
33 |
| - int readLength = buffer.Length; |
34 |
| - if (bytesRemaining.HasValue) |
35 |
| - { |
36 |
| - readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength); |
37 |
| - } |
38 |
| - int count = await source.ReadAsync(buffer, 0, readLength, cancel); |
| 40 | + int readLength = buffer.Length; |
| 41 | + if (bytesRemaining.HasValue) |
| 42 | + { |
| 43 | + readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength); |
| 44 | + } |
| 45 | + int count = await source.ReadAsync(buffer, 0, readLength, cancel); |
39 | 46 |
|
40 |
| - if (bytesRemaining.HasValue) |
41 |
| - { |
42 |
| - bytesRemaining -= count; |
43 |
| - } |
| 47 | + if (bytesRemaining.HasValue) |
| 48 | + { |
| 49 | + bytesRemaining -= count; |
| 50 | + } |
44 | 51 |
|
45 |
| - // End of the source stream. |
46 |
| - if (count == 0) |
47 |
| - { |
48 |
| - return; |
49 |
| - } |
| 52 | + // End of the source stream. |
| 53 | + if (count == 0) |
| 54 | + { |
| 55 | + return; |
| 56 | + } |
50 | 57 |
|
51 |
| - cancel.ThrowIfCancellationRequested(); |
| 58 | + cancel.ThrowIfCancellationRequested(); |
52 | 59 |
|
53 |
| - await destination.WriteAsync(buffer, 0, count, cancel); |
| 60 | + await destination.WriteAsync(buffer, 0, count, cancel); |
| 61 | + } |
| 62 | + } |
| 63 | + finally |
| 64 | + { |
| 65 | + ArrayPool<byte>.Shared.Return(buffer); |
54 | 66 | }
|
55 | 67 | }
|
56 | 68 | }
|
|
0 commit comments