| | | 1 | | #pragma warning disable CS1591 |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Buffers; |
| | | 5 | | using System.IO; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using MediaBrowser.Model.IO; |
| | | 9 | | |
| | | 10 | | namespace Jellyfin.LiveTv.IO |
| | | 11 | | { |
| | | 12 | | public class StreamHelper : IStreamHelper |
| | | 13 | | { |
| | | 14 | | public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, Action? onStarted, Cancellation |
| | | 15 | | { |
| | 0 | 16 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); |
| | | 17 | | try |
| | | 18 | | { |
| | | 19 | | int read; |
| | 0 | 20 | | while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) |
| | | 21 | | { |
| | 0 | 22 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 23 | | |
| | 0 | 24 | | await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); |
| | | 25 | | |
| | 0 | 26 | | if (onStarted is not null) |
| | | 27 | | { |
| | 0 | 28 | | onStarted(); |
| | 0 | 29 | | onStarted = null; |
| | | 30 | | } |
| | | 31 | | } |
| | 0 | 32 | | } |
| | | 33 | | finally |
| | | 34 | | { |
| | 0 | 35 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 36 | | } |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, int emptyReadLimit, Cancellatio |
| | | 40 | | { |
| | 0 | 41 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); |
| | | 42 | | try |
| | | 43 | | { |
| | 0 | 44 | | if (emptyReadLimit <= 0) |
| | | 45 | | { |
| | | 46 | | int read; |
| | 0 | 47 | | while ((read = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) |
| | | 48 | | { |
| | 0 | 49 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 50 | | |
| | 0 | 51 | | await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false); |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | return; |
| | | 55 | | } |
| | | 56 | | |
| | 0 | 57 | | var eofCount = 0; |
| | | 58 | | |
| | 0 | 59 | | while (eofCount < emptyReadLimit) |
| | | 60 | | { |
| | 0 | 61 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 62 | | |
| | 0 | 63 | | var bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false); |
| | | 64 | | |
| | 0 | 65 | | if (bytesRead == 0) |
| | | 66 | | { |
| | 0 | 67 | | eofCount++; |
| | 0 | 68 | | await Task.Delay(50, cancellationToken).ConfigureAwait(false); |
| | | 69 | | } |
| | | 70 | | else |
| | | 71 | | { |
| | 0 | 72 | | eofCount = 0; |
| | | 73 | | |
| | 0 | 74 | | await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(fa |
| | | 75 | | } |
| | | 76 | | } |
| | 0 | 77 | | } |
| | | 78 | | finally |
| | | 79 | | { |
| | 0 | 80 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 81 | | } |
| | 0 | 82 | | } |
| | | 83 | | |
| | | 84 | | public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellatio |
| | | 85 | | { |
| | 0 | 86 | | byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize); |
| | | 87 | | try |
| | | 88 | | { |
| | 0 | 89 | | while (!cancellationToken.IsCancellationRequested) |
| | | 90 | | { |
| | 0 | 91 | | var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait( |
| | | 92 | | |
| | 0 | 93 | | if (bytesRead == 0) |
| | | 94 | | { |
| | 0 | 95 | | await Task.Delay(100, cancellationToken).ConfigureAwait(false); |
| | | 96 | | } |
| | | 97 | | } |
| | 0 | 98 | | } |
| | | 99 | | finally |
| | | 100 | | { |
| | 0 | 101 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 102 | | } |
| | 0 | 103 | | } |
| | | 104 | | |
| | | 105 | | private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, Cancellatio |
| | | 106 | | { |
| | | 107 | | int bytesRead; |
| | 0 | 108 | | int totalBytesRead = 0; |
| | | 109 | | |
| | 0 | 110 | | while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0) |
| | | 111 | | { |
| | 0 | 112 | | await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false); |
| | | 113 | | |
| | 0 | 114 | | totalBytesRead += bytesRead; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | return totalBytesRead; |
| | 0 | 118 | | } |
| | | 119 | | } |
| | | 120 | | } |