From c30e9f4efd921a1bc1ad2f30788b6ec781ee256f Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Fri, 5 Feb 2016 00:26:01 +0000 Subject: [PATCH] cr feedback --- .../FileBufferingReadStream.cs | 1 + .../MultipartReaderStream.cs | 4 ++-- .../StreamHelperExtensions.cs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs b/src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs index 47525dc6..07deca41 100644 --- a/src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs +++ b/src/Microsoft.AspNetCore.WebUtilities/FileBufferingReadStream.cs @@ -73,6 +73,7 @@ public FileBufferingReadStream( // TODO: allow for an optional buffer size limit to prevent filling hard disks. 1gb? public FileBufferingReadStream(Stream inner, int memoryThreshold, string tempFileDirectory) + : this (inner, memoryThreshold, tempFileDirectory, ArrayPool.Shared) { } diff --git a/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs b/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs index 3715120e..02738e43 100644 --- a/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs +++ b/src/Microsoft.AspNetCore.WebUtilities/MultipartReaderStream.cs @@ -242,6 +242,7 @@ public override int Read(byte[] buffer, int offset, int count) // or -- for the final boundary. var boundary = _bytePool.Rent(_boundaryBytes.Length); read = _innerStream.Read(boundary, 0, _boundaryBytes.Length); + _bytePool.Return(boundary); Debug.Assert(read == _boundaryBytes.Length); // It should have all been buffered var remainder = _innerStream.ReadLine(lengthLimit: 100); // Whitespace may exceed the buffer. remainder = remainder.Trim(); @@ -249,7 +250,6 @@ public override int Read(byte[] buffer, int offset, int count) { FinalBoundaryFound = true; } - _bytePool.Return(boundary); Debug.Assert(FinalBoundaryFound || string.Equals(string.Empty, remainder, StringComparison.Ordinal), "Un-expected data found on the boundary line: " + remainder); _finished = true; return 0; @@ -294,6 +294,7 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, // or -- for the final boundary. var boundary = _bytePool.Rent(_boundaryBytes.Length); read = _innerStream.Read(boundary, 0, _boundaryBytes.Length); + _bytePool.Return(boundary); Debug.Assert(read == _boundaryBytes.Length); // It should have all been buffered var remainder = await _innerStream.ReadLineAsync(lengthLimit: 100, cancellationToken: cancellationToken); // Whitespace may exceed the buffer. remainder = remainder.Trim(); @@ -301,7 +302,6 @@ public override async Task ReadAsync(byte[] buffer, int offset, int count, { FinalBoundaryFound = true; } - _bytePool.Return(boundary); Debug.Assert(FinalBoundaryFound || string.Equals(string.Empty, remainder, StringComparison.Ordinal), "Un-expected data found on the boundary line: " + remainder); _finished = true; diff --git a/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs b/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs index 4c8b4af8..ffd8435c 100644 --- a/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs +++ b/src/Microsoft.AspNetCore.WebUtilities/StreamHelperExtensions.cs @@ -12,9 +12,9 @@ public static class StreamHelperExtensions { public static Task DrainAsync(this Stream stream, CancellationToken cancellationToken) { - return stream.DrainAsync(cancellationToken, ArrayPool.Shared); + return stream.DrainAsync(ArrayPool.Shared, cancellationToken); } - public static async Task DrainAsync(this Stream stream, CancellationToken cancellationToken, ArrayPool bytePool) + public static async Task DrainAsync(this Stream stream, ArrayPool bytePool, CancellationToken cancellationToken) { cancellationToken.ThrowIfCancellationRequested(); var buffer = bytePool.Rent(1024);