mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-07-07 18:24:19 -04:00
Merge pull request #4022 from Bond-009/arraypool
Fix incorrect usage of ArrayPool
This commit is contained in:
commit
43a81366a6
@ -130,34 +130,10 @@ namespace Jellyfin.Api.Helpers
|
|||||||
private async Task<int> CopyToInternalAsync(Stream source, Stream destination, bool readAsync, CancellationToken cancellationToken)
|
private async Task<int> CopyToInternalAsync(Stream source, Stream destination, bool readAsync, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
var array = ArrayPool<byte>.Shared.Rent(IODefaults.CopyToBufferSize);
|
var array = ArrayPool<byte>.Shared.Rent(IODefaults.CopyToBufferSize);
|
||||||
int bytesRead;
|
try
|
||||||
int totalBytesRead = 0;
|
|
||||||
|
|
||||||
if (readAsync)
|
|
||||||
{
|
{
|
||||||
bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false);
|
int bytesRead;
|
||||||
}
|
int totalBytesRead = 0;
|
||||||
else
|
|
||||||
{
|
|
||||||
bytesRead = source.Read(array, 0, array.Length);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (bytesRead != 0)
|
|
||||||
{
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (readAsync)
|
if (readAsync)
|
||||||
{
|
{
|
||||||
@ -167,9 +143,40 @@ namespace Jellyfin.Api.Helpers
|
|||||||
{
|
{
|
||||||
bytesRead = source.Read(array, 0, array.Length);
|
bytesRead = source.Read(array, 0, array.Length);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return totalBytesRead;
|
while (bytesRead != 0)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (readAsync)
|
||||||
|
{
|
||||||
|
bytesRead = await source.ReadAsync(array, 0, array.Length, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
bytesRead = source.Read(array, 0, array.Length);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return totalBytesRead;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
ArrayPool<byte>.Shared.Return(array);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user