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