From d0ce239e3e71d52fd405cd7981cecb5ee49983a5 Mon Sep 17 00:00:00 2001 From: crobibero Date: Wed, 29 Jul 2020 12:12:54 -0600 Subject: [PATCH] Use ArrayPool and reduce duplicate code --- Jellyfin.Api/Helpers/ProgressiveFileCopier.cs | 59 +++++++------------ 1 file changed, 21 insertions(+), 38 deletions(-) diff --git a/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs b/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs index d8b1828f5d..71197d931f 100644 --- a/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs +++ b/Jellyfin.Api/Helpers/ProgressiveFileCopier.cs @@ -93,23 +93,15 @@ namespace Jellyfin.Api.Helpers await using var inputStream = new FileStream(_path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, IODefaults.FileStreamBufferSize, fileOptions); var eofCount = 0; - const int emptyReadLimit = 20; + const int EmptyReadLimit = 20; if (StartPosition > 0) { inputStream.Position = StartPosition; } - while (eofCount < emptyReadLimit || !AllowEndOfFile) + while (eofCount < EmptyReadLimit || !AllowEndOfFile) { - int bytesRead; - if (allowAsyncFileRead) - { - bytesRead = await CopyToInternalAsync(inputStream, outputStream, cancellationToken).ConfigureAwait(false); - } - else - { - bytesRead = await CopyToInternalAsyncWithSyncRead(inputStream, outputStream, cancellationToken).ConfigureAwait(false); - } + var bytesRead = await CopyToInternalAsync(inputStream, outputStream, allowAsyncFileRead, cancellationToken).ConfigureAwait(false); if (bytesRead == 0) { @@ -135,40 +127,22 @@ namespace Jellyfin.Api.Helpers } } - private async Task CopyToInternalAsyncWithSyncRead(Stream source, Stream destination, CancellationToken cancellationToken) + private async Task CopyToInternalAsync(Stream source, Stream destination, bool readAsync, CancellationToken cancellationToken) { - var array = new byte[IODefaults.CopyToBufferSize]; + var array = ArrayPool.Shared.Rent(IODefaults.CopyToBufferSize); int bytesRead; int totalBytesRead = 0; - while ((bytesRead = source.Read(array, 0, array.Length)) != 0) + if (readAsync) { - var bytesToWrite = bytesRead; - - if (bytesToWrite > 0) - { - await destination.WriteAsync(array, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false); - - _bytesWritten += bytesRead; - totalBytesRead += bytesRead; - - if (_job != null) - { - _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten); - } - } + bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false); + } + else + { + bytesRead = source.Read(array, 0, array.Length); } - return totalBytesRead; - } - - private async Task CopyToInternalAsync(Stream source, Stream destination, CancellationToken cancellationToken) - { - var array = new byte[IODefaults.CopyToBufferSize]; - int bytesRead; - int totalBytesRead = 0; - - while ((bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false)) != 0) + while (bytesRead != 0) { var bytesToWrite = bytesRead; @@ -184,6 +158,15 @@ namespace Jellyfin.Api.Helpers _job.BytesDownloaded = Math.Max(_job.BytesDownloaded ?? _bytesWritten, _bytesWritten); } } + + if (readAsync) + { + bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false); + } + else + { + bytesRead = source.Read(array, 0, array.Length); + } } return totalBytesRead;