StreamHelper.cs 6.1 KB

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