StreamHelper.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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, cancellationToken).ConfigureAwait(false)) != 0)
  19. {
  20. cancellationToken.ThrowIfCancellationRequested();
  21. await destination.WriteAsync(buffer, 0, read, cancellationToken).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, cancellationToken).ConfigureAwait(false)) != 0)
  43. {
  44. cancellationToken.ThrowIfCancellationRequested();
  45. await destination.WriteAsync(buffer, 0, read, cancellationToken).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, cancellationToken).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, cancellationToken).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 CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
  95. {
  96. byte[] buffer = ArrayPool<byte>.Shared.Rent(StreamCopyToBufferSize);
  97. try
  98. {
  99. int bytesRead;
  100. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  101. {
  102. var bytesToWrite = Math.Min(bytesRead, copyLength);
  103. if (bytesToWrite > 0)
  104. {
  105. await destination.WriteAsync(buffer, 0, Convert.ToInt32(bytesToWrite), cancellationToken).ConfigureAwait(false);
  106. }
  107. copyLength -= bytesToWrite;
  108. if (copyLength <= 0)
  109. {
  110. break;
  111. }
  112. }
  113. }
  114. finally
  115. {
  116. ArrayPool<byte>.Shared.Return(buffer);
  117. }
  118. }
  119. public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellationToken)
  120. {
  121. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  122. try
  123. {
  124. while (!cancellationToken.IsCancellationRequested)
  125. {
  126. var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
  127. if (bytesRead == 0)
  128. {
  129. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  130. }
  131. }
  132. }
  133. finally
  134. {
  135. ArrayPool<byte>.Shared.Return(buffer);
  136. }
  137. }
  138. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken)
  139. {
  140. int bytesRead;
  141. int totalBytesRead = 0;
  142. while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length, cancellationToken).ConfigureAwait(false)) != 0)
  143. {
  144. await destination.WriteAsync(buffer, 0, bytesRead, cancellationToken).ConfigureAwait(false);
  145. totalBytesRead += bytesRead;
  146. }
  147. return totalBytesRead;
  148. }
  149. }
  150. }