StreamHelper.cs 968 B

1234567891011121314151617181920212223242526272829303132
  1. using System.IO;
  2. using System.Threading;
  3. using System;
  4. namespace MediaBrowser.Controller.IO
  5. {
  6. public static class StreamHelper
  7. {
  8. public static void CopyTo(Stream source, Stream destination, int bufferSize, CancellationToken cancellationToken)
  9. {
  10. CopyTo(source, destination, bufferSize, null, cancellationToken);
  11. }
  12. public static void CopyTo(Stream source, Stream destination, int bufferSize, Action onStarted, CancellationToken cancellationToken)
  13. {
  14. byte[] buffer = new byte[bufferSize];
  15. int read;
  16. while ((read = source.Read(buffer, 0, buffer.Length)) != 0)
  17. {
  18. cancellationToken.ThrowIfCancellationRequested();
  19. destination.Write(buffer, 0, read);
  20. if (onStarted != null)
  21. {
  22. onStarted();
  23. onStarted = null;
  24. }
  25. }
  26. }
  27. }
  28. }