Use ArrayPool.Shared for StreamCopyOperation
This commit is contained in:
parent
28cc5a4517
commit
a9f4969cfc
|
|
@ -2,6 +2,7 @@
|
||||||
// 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;
|
||||||
|
|
@ -12,13 +13,15 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
// 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
|
internal static class StreamCopyOperation
|
||||||
{
|
{
|
||||||
private const int DefaultBufferSize = 1024 * 16;
|
private const int DefaultBufferSize = 4096;
|
||||||
|
|
||||||
internal static async Task CopyToAsync(Stream source, Stream destination, long? length, CancellationToken cancel)
|
internal static async Task CopyToAsync(Stream source, Stream destination, long? length, CancellationToken cancel)
|
||||||
{
|
{
|
||||||
long? bytesRemaining = length;
|
long? bytesRemaining = length;
|
||||||
byte[] buffer = new byte[DefaultBufferSize];
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
@ -57,5 +60,10 @@ namespace Microsoft.AspNet.StaticFiles
|
||||||
await destination.WriteAsync(buffer, 0, count, cancel);
|
await destination.WriteAsync(buffer, 0, count, cancel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ArrayPool<byte>.Shared.Return(buffer);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -14,7 +14,8 @@
|
||||||
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*",
|
"Microsoft.AspNet.FileProviders.Abstractions": "1.0.0-*",
|
||||||
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",
|
"Microsoft.AspNet.Hosting.Abstractions": "1.0.0-*",
|
||||||
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*",
|
"Microsoft.Extensions.Logging.Abstractions": "1.0.0-*",
|
||||||
"Microsoft.Extensions.WebEncoders": "1.0.0-*"
|
"Microsoft.Extensions.WebEncoders": "1.0.0-*",
|
||||||
|
"System.Buffers": "4.0.0-*"
|
||||||
},
|
},
|
||||||
"frameworks": {
|
"frameworks": {
|
||||||
"net451": {},
|
"net451": {},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue