This repository has been archived by the owner on Nov 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 194
/
StreamCopyOperation.cs
87 lines (75 loc) · 3.91 KB
/
StreamCopyOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Buffers;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.AspNetCore.Http.Extensions
{
// FYI: In most cases the source will be a FileStream and the destination will be to the network.
public static class StreamCopyOperation
{
private const int DefaultBufferSize = 4096;
/// <summary>Asynchronously reads the bytes from the source stream and writes them to another stream.</summary>
/// <returns>A task that represents the asynchronous copy operation.</returns>
/// <param name="source">The stream from which the contents will be copied.</param>
/// <param name="destination">The stream to which the contents of the current stream will be copied.</param>
/// <param name="count">The count of bytes to be copied.</param>
/// <param name="cancel">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
public static Task CopyToAsync(Stream source, Stream destination, long? count, CancellationToken cancel)
{
return CopyToAsync(source, destination, count, DefaultBufferSize, cancel);
}
/// <summary>Asynchronously reads the bytes from the source stream and writes them to another stream, using a specified buffer size.</summary>
/// <returns>A task that represents the asynchronous copy operation.</returns>
/// <param name="source">The stream from which the contents will be copied.</param>
/// <param name="destination">The stream to which the contents of the current stream will be copied.</param>
/// <param name="count">The count of bytes to be copied.</param>
/// <param name="bufferSize">The size, in bytes, of the buffer. This value must be greater than zero. The default size is 4096.</param>
/// <param name="cancel">The token to monitor for cancellation requests. The default value is <see cref="P:System.Threading.CancellationToken.None" />.</param>
public static async Task CopyToAsync(Stream source, Stream destination, long? count, int bufferSize, CancellationToken cancel)
{
long? bytesRemaining = count;
var buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
try
{
Debug.Assert(source != null);
Debug.Assert(destination != null);
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0);
Debug.Assert(buffer != null);
while (true)
{
// The natural end of the range.
if (bytesRemaining.HasValue && bytesRemaining.Value <= 0)
{
return;
}
cancel.ThrowIfCancellationRequested();
int readLength = buffer.Length;
if (bytesRemaining.HasValue)
{
readLength = (int)Math.Min(bytesRemaining.Value, (long)readLength);
}
int read = await source.ReadAsync(buffer, 0, readLength, cancel);
if (bytesRemaining.HasValue)
{
bytesRemaining -= read;
}
// End of the source stream.
if (read == 0)
{
return;
}
cancel.ThrowIfCancellationRequested();
await destination.WriteAsync(buffer, 0, read, cancel);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
}