StreamHelper.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. 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, cancellationToken).ConfigureAwait(false)) != 0)
  19. {
  20. cancellationToken.ThrowIfCancellationRequested();
  21. await destination.WriteAsync(buffer.AsMemory(0, read), cancellationToken).ConfigureAwait(false);
  22. if (onStarted is not 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, cancellationToken).ConfigureAwait(false)) != 0)
  43. {
  44. cancellationToken.ThrowIfCancellationRequested();
  45. await destination.WriteAsync(buffer.AsMemory(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, 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.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
  63. }
  64. }
  65. }
  66. finally
  67. {
  68. ArrayPool<byte>.Shared.Return(buffer);
  69. }
  70. }
  71. public async Task CopyToAsync(Stream source, Stream destination, long copyLength, CancellationToken cancellationToken)
  72. {
  73. byte[] buffer = ArrayPool<byte>.Shared.Rent(IODefaults.CopyToBufferSize);
  74. try
  75. {
  76. int bytesRead;
  77. while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
  78. {
  79. var bytesToWrite = Math.Min(bytesRead, copyLength);
  80. if (bytesToWrite > 0)
  81. {
  82. await destination.WriteAsync(buffer.AsMemory(0, Convert.ToInt32(bytesToWrite)), cancellationToken).ConfigureAwait(false);
  83. }
  84. copyLength -= bytesToWrite;
  85. if (copyLength <= 0)
  86. {
  87. break;
  88. }
  89. }
  90. }
  91. finally
  92. {
  93. ArrayPool<byte>.Shared.Return(buffer);
  94. }
  95. }
  96. public async Task CopyUntilCancelled(Stream source, Stream target, int bufferSize, CancellationToken cancellationToken)
  97. {
  98. byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferSize);
  99. try
  100. {
  101. while (!cancellationToken.IsCancellationRequested)
  102. {
  103. var bytesRead = await CopyToAsyncInternal(source, target, buffer, cancellationToken).ConfigureAwait(false);
  104. if (bytesRead == 0)
  105. {
  106. await Task.Delay(100, cancellationToken).ConfigureAwait(false);
  107. }
  108. }
  109. }
  110. finally
  111. {
  112. ArrayPool<byte>.Shared.Return(buffer);
  113. }
  114. }
  115. private static async Task<int> CopyToAsyncInternal(Stream source, Stream destination, byte[] buffer, CancellationToken cancellationToken)
  116. {
  117. int bytesRead;
  118. int totalBytesRead = 0;
  119. while ((bytesRead = await source.ReadAsync(buffer, cancellationToken).ConfigureAwait(false)) != 0)
  120. {
  121. await destination.WriteAsync(buffer.AsMemory(0, bytesRead), cancellationToken).ConfigureAwait(false);
  122. totalBytesRead += bytesRead;
  123. }
  124. return totalBytesRead;
  125. }
  126. }
  127. }