Make StreamCopyOperation public and update it to same as StaticFiles

This commit is contained in:
Brennan 2016-01-14 14:38:42 -08:00
parent 1f21540fd5
commit 3f84e992f4
3 changed files with 51 additions and 40 deletions

View File

@ -5,6 +5,7 @@ using System;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNet.Http.Extensions;
using Microsoft.AspNet.Http.Features; using Microsoft.AspNet.Http.Features;
namespace Microsoft.AspNet.Http namespace Microsoft.AspNet.Http
@ -96,10 +97,7 @@ namespace Microsoft.AspNet.Http
{ {
fileStream.Seek(offset, SeekOrigin.Begin); fileStream.Seek(offset, SeekOrigin.Begin);
// TODO: Use buffer pool await StreamCopyOperation.CopyToAsync(fileStream, outputStream, length, cancel);
var buffer = new byte[bufferSize];
await StreamCopyOperation.CopyToAsync(fileStream, buffer, outputStream, length, cancel);
} }
} }
} }

View File

@ -2,19 +2,26 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System; using System;
using System.Buffers;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Microsoft.AspNet.Http namespace Microsoft.AspNet.Http.Extensions
{ {
// FYI: In most cases the source will be a FileStream and the destination will be to the network. // FYI: In most cases the source will be a FileStream and the destination will be to the network.
internal static class StreamCopyOperation public static class StreamCopyOperation
{ {
internal static async Task CopyToAsync(Stream source, byte[] buffer, Stream destination, long? length, CancellationToken cancel) private const int DefaultBufferSize = 4096;
public static async Task CopyToAsync(Stream source, Stream destination, long? length, CancellationToken cancel)
{ {
long? bytesRemaining = length; long? bytesRemaining = length;
var buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize);
try
{
Debug.Assert(source != null); Debug.Assert(source != null);
Debug.Assert(destination != null); Debug.Assert(destination != null);
Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0); Debug.Assert(!bytesRemaining.HasValue || bytesRemaining.Value >= 0);
@ -53,5 +60,10 @@ namespace Microsoft.AspNet.Http
await destination.WriteAsync(buffer, 0, count, cancel); await destination.WriteAsync(buffer, 0, count, cancel);
} }
} }
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
} }
} }

View File

@ -11,7 +11,8 @@
}, },
"dependencies": { "dependencies": {
"Microsoft.AspNet.Http.Abstractions": "1.0.0-*", "Microsoft.AspNet.Http.Abstractions": "1.0.0-*",
"Microsoft.Net.Http.Headers": "1.0.0-*" "Microsoft.Net.Http.Headers": "1.0.0-*",
"System.Buffers": "4.0.0-*"
}, },
"frameworks": { "frameworks": {
"net451": {}, "net451": {},