Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.

Commit 3f84e99

Browse files
committed
Make StreamCopyOperation public and update it to same as StaticFiles
1 parent 1f21540 commit 3f84e99

File tree

3 files changed

+46
-35
lines changed

3 files changed

+46
-35
lines changed

src/Microsoft.AspNet.Http.Extensions/SendFileResponseExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Threading;
77
using System.Threading.Tasks;
8+
using Microsoft.AspNet.Http.Extensions;
89
using Microsoft.AspNet.Http.Features;
910

1011
namespace Microsoft.AspNet.Http
@@ -96,10 +97,7 @@ private static async Task SendFileAsync(Stream outputStream, string fileName, lo
9697
{
9798
fileStream.Seek(offset, SeekOrigin.Begin);
9899

99-
// TODO: Use buffer pool
100-
var buffer = new byte[bufferSize];
101-
102-
await StreamCopyOperation.CopyToAsync(fileStream, buffer, outputStream, length, cancel);
100+
await StreamCopyOperation.CopyToAsync(fileStream, outputStream, length, cancel);
103101
}
104102
}
105103
}

src/Microsoft.AspNet.Http.Extensions/StreamCopyOperation.cs

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,55 +2,67 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Buffers;
56
using System.Diagnostics;
67
using System.IO;
78
using System.Threading;
89
using System.Threading.Tasks;
910

10-
namespace Microsoft.AspNet.Http
11+
namespace Microsoft.AspNet.Http.Extensions
1112
{
1213
// 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
1415
{
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)
1619
{
1720
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);
2221

23-
while (true)
22+
var buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize);
23+
try
2424
{
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)
2731
{
28-
return;
29-
}
32+
// The natural end of the range.
33+
if (bytesRemaining.HasValue && bytesRemaining.Value <= 0)
34+
{
35+
return;
36+
}
3037

31-
cancel.ThrowIfCancellationRequested();
38+
cancel.ThrowIfCancellationRequested();
3239

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

40-
if (bytesRemaining.HasValue)
41-
{
42-
bytesRemaining -= count;
43-
}
47+
if (bytesRemaining.HasValue)
48+
{
49+
bytesRemaining -= count;
50+
}
4451

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+
}
5057

51-
cancel.ThrowIfCancellationRequested();
58+
cancel.ThrowIfCancellationRequested();
5259

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);
5466
}
5567
}
5668
}

src/Microsoft.AspNet.Http.Extensions/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
},
1212
"dependencies": {
1313
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*",
14-
"Microsoft.Net.Http.Headers": "1.0.0-*"
14+
"Microsoft.Net.Http.Headers": "1.0.0-*",
15+
"System.Buffers": "4.0.0-*"
1516
},
1617
"frameworks": {
1718
"net451": {},

0 commit comments

Comments
 (0)