StreamHelper.cs 6.3 KB

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