StreamExtensions.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace ILRuntime.Mono
  7. {
  8. static class StreamExtensions
  9. {
  10. public static void CopyTo(this Stream src, Stream destination)
  11. {
  12. if (destination == null)
  13. {
  14. throw new ArgumentNullException("destination");
  15. }
  16. if (!src.CanRead && !src.CanWrite)
  17. {
  18. throw new ObjectDisposedException(null, "ObjectDisposed_StreamClosed");
  19. }
  20. if (!destination.CanRead && !destination.CanWrite)
  21. {
  22. throw new ObjectDisposedException("destination", "ObjectDisposed_StreamClosed");
  23. }
  24. if (!src.CanRead)
  25. {
  26. throw new NotSupportedException("NotSupported_UnreadableStream");
  27. }
  28. if (!destination.CanWrite)
  29. {
  30. throw new NotSupportedException("NotSupported_UnwritableStream");
  31. }
  32. InternalCopyTo(src, destination, 81920);
  33. }
  34. static void InternalCopyTo(Stream src, Stream destination, int bufferSize)
  35. {
  36. byte[] array = new byte[bufferSize];
  37. int count;
  38. while ((count = src.Read(array, 0, array.Length)) != 0)
  39. {
  40. destination.Write(array, 0, count);
  41. }
  42. }
  43. }
  44. }