StreamHelper.cs 7.8 KB

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