Use HttpAbstractions StreamCopy

This commit is contained in:
Brennan 2016-01-19 09:33:20 -08:00
parent 2c1317d3c6
commit 9c6b081ebc
3 changed files with 2 additions and 71 deletions

View File

@ -10,6 +10,7 @@ using System.Threading.Tasks;
using Microsoft.AspNet.Builder;
using Microsoft.AspNet.FileProviders;
using Microsoft.AspNet.Http;
using Microsoft.AspNet.Http.Extensions;
using Microsoft.AspNet.Http.Features;
using Microsoft.AspNet.Http.Headers;
using Microsoft.AspNet.StaticFiles.Infrastructure;

View File

@ -1,69 +0,0 @@
// 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.AspNet.StaticFiles
{
// FYI: In most cases the source will be a FileStream and the destination will be to the network.
internal static class StreamCopyOperation
{
private const int DefaultBufferSize = 4096;
internal static async Task CopyToAsync(Stream source, Stream destination, long? length, CancellationToken cancel)
{
long? bytesRemaining = length;
var buffer = ArrayPool<byte>.Shared.Rent(DefaultBufferSize);
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 count = await source.ReadAsync(buffer, 0, readLength, cancel);
if (bytesRemaining.HasValue)
{
bytesRemaining -= count;
}
// End of the source stream.
if (count == 0)
{
return;
}
cancel.ThrowIfCancellationRequested();
await destination.WriteAsync(buffer, 0, count, cancel);
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
}
}

View File

@ -14,8 +14,7 @@
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*",
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*",
"Microsoft.Extensions.WebEncoders": "1.0.0-*",
"System.Buffers": "4.0.0-*"
"Microsoft.Extensions.WebEncoders": "1.0.0-*"
},
"frameworks": {
"net451": {},