2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2019-03-13 09:51:33 -07:00
|
|
|
using System.Buffers;
|
2019-01-13 12:21:32 -07:00
|
|
|
using System.IO;
|
2018-09-12 10:26:21 -07:00
|
|
|
using System.Threading;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using MediaBrowser.Model.IO;
|
|
|
|
|
|
|
|
namespace Emby.Server.Implementations.IO
|
|
|
|
{
|
|
|
|
public class StreamHelper : IStreamHelper
|
|
|
|
{
|
2021-05-20 12:28:18 -07:00
|
|
|
public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, Action? onStarted, CancellationToken cancellationToken)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
|
|
|
try
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
int read;
|
2021-12-24 14:18:24 -07:00
|
|
|
while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
|
2019-03-13 09:51:33 -07:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
if (onStarted != null)
|
|
|
|
{
|
|
|
|
onStarted();
|
|
|
|
onStarted = null;
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 09:51:33 -07:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, int emptyReadLimit, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
|
|
|
try
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
if (emptyReadLimit <= 0)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
int read;
|
2021-12-24 14:18:24 -07:00
|
|
|
while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
|
2019-03-13 09:51:33 -07:00
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
|
2019-03-13 09:51:33 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
return;
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
var eofCount = 0;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
while (eofCount < emptyReadLimit)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
var bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
if (bytesRead == 0)
|
|
|
|
{
|
|
|
|
eofCount++;
|
|
|
|
await Task.Delay(50, cancellationToken).ConfigureAwait(false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eofCount = 0;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
|
2019-03-13 09:51:33 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 09:51:33 -07:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
|
|
|
|
{
|
2020-08-31 13:20:19 -07:00
|
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(IODefaults.CopyToBufferSize);
|
2019-03-13 09:51:33 -07:00
|
|
|
try
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
int bytesRead;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
var bytesToWrite = Math.Min(bytesRead, copyLength);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
if (bytesToWrite > 0)
|
|
|
|
{
|
2021-12-24 14:18:24 -07:00
|
|
|
await destination.WriteAsync(buffer.AsMemory(0, Convert.ToInt32(bytesToWrite)), cancellationToken).ConfigureAwait(false);
|
2019-03-13 09:51:33 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-13 09:51:33 -07:00
|
|
|
copyLength -= bytesToWrite;
|
|
|
|
|
|
|
|
if (copyLength <= 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 09:51:33 -07:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellationToken)
|
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
|
|
|
|
try
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
while (!cancellationToken.IsCancellationRequested)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2019-03-13 09:51:33 -07:00
|
|
|
var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
|
|
|
|
|
|
|
|
if (bytesRead == 0)
|
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
await Task.Delay(100, cancellationToken).ConfigureAwait(false);
|
2019-03-13 09:51:33 -07:00
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
2019-03-13 09:51:33 -07:00
|
|
|
finally
|
|
|
|
{
|
|
|
|
ArrayPool<byte>.Shared.Return(buffer);
|
|
|
|
}
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
int bytesRead;
|
|
|
|
int totalBytesRead = 0;
|
|
|
|
|
2021-12-24 14:18:24 -07:00
|
|
|
while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2021-12-24 14:18:24 -07:00
|
|
|
await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
|
2018-09-12 10:26:21 -07:00
|
|
|
|
|
|
|
totalBytesRead += bytesRead;
|
|
|
|
}
|
|
|
|
|
|
|
|
return totalBytesRead;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|