StreamHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #pragma warning disable CS1591
  2. #pragma warning disable SA1600
  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. namespace Emby.Server.Implementations.IO
  10. {
  11. public class StreamHelper : IStreamHelper
  12. {
  13. private const int StreamCopyToBufferSize = 81920;
  14. public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken)
  15. {
  16. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  17. try
  18. {
  19. int read;
  20. while ((read = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  21. {
  22. cancellationToken.ThrowIfCancellationRequested();
  23. await destination.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);
  24. if (onStarted != null)
  25. {
  26. onStarted();
  27. onStarted = null;
  28. }
  29. }
  30. }
  31. finally
  32. {
  33. ArrayPool<byte>.Shared.Return(buffer);
  34. }
  35. }
  36. public async Task CopyToAsync(Stream source, Stream destination, int bufferSize, int emptyReadLimit, CancellationToken cancellationToken)
  37. {
  38. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  39. try
  40. {
  41. if (emptyReadLimit <= 0)
  42. {
  43. int read;
  44. while ((read = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  45. {
  46. cancellationToken.ThrowIfCancellationRequested();
  47. await destination.WriteAsync(buffer, 0, read, cancellationToken).ConfigureAwait(false);
  48. }
  49. return;
  50. }
  51. var eofCount = 0;
  52. while (eofCount < emptyReadLimit)
  53. {
  54. cancellationToken.ThrowIfCancellationRequested();
  55. var bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false);
  56. if (bytesRead == 0)
  57. {
  58. eofCount++;
  59. await Task.Delay(50, cancellationToken).ConfigureAwait(false);
  60. }
  61. else
  62. {
  63. eofCount = 0;
  64. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  65. }
  66. }
  67. }
  68. finally
  69. {
  70. ArrayPool<byte>.Shared.Return(buffer);
  71. }
  72. }
  73. public async Task<int> CopyToAsync(Stream source, Stream destination, CancellationToken cancellationToken)
  74. {
  75. byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
  76. try
  77. {
  78. int totalBytesRead = 0;
  79. int bytesRead;
  80. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  81. {
  82. var bytesToWrite = bytesRead;
  83. if (bytesToWrite > 0)
  84. {
  85. await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  86. totalBytesRead += bytesRead;
  87. }
  88. }
  89. return totalBytesRead;
  90. }
  91. finally
  92. {
  93. ArrayPool<byte>.Shared.Return(buffer);
  94. }
  95. }
  96. public async Task CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
  97. {
  98. byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
  99. try
  100. {
  101. int bytesRead;
  102. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  103. {
  104. var bytesToWrite = Math.Min(bytesRead, copyLength);
  105. if (bytesToWrite > 0)
  106. {
  107. await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  108. }
  109. copyLength -= bytesToWrite;
  110. if (copyLength <= 0)
  111. {
  112. break;
  113. }
  114. }
  115. }
  116. finally
  117. {
  118. ArrayPool<byte>.Shared.Return(buffer);
  119. }
  120. }
  121. public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellationToken)
  122. {
  123. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  124. try
  125. {
  126. while (!cancellationToken.IsCancellationRequested)
  127. {
  128. var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
  129. if (bytesRead == 0)
  130. {
  131. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  132. }
  133. }
  134. }
  135. finally
  136. {
  137. ArrayPool<byte>.Shared.Return(buffer);
  138. }
  139. }
  140. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken)
  141. {
  142. int bytesRead;
  143. int totalBytesRead = 0;
  144. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  145. {
  146. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  147. totalBytesRead += bytesRead;
  148. }
  149. return totalBytesRead;
  150. }
  151. }
  152. }