StreamExtensions.cs 911 B

1234567891011121314151617181920212223242526272829303132
  1. using System.IO;
  2. namespace UnityFS
  3. {
  4. public static class StreamExtensions
  5. {
  6. private const int BufferSize = 81920;
  7. public static void CopyTo(this Stream source, Stream destination, long size)
  8. {
  9. var buffer = new byte[BufferSize];
  10. for (var left = size; left > 0; left -= BufferSize)
  11. {
  12. int toRead = BufferSize < left ? BufferSize : (int)left;
  13. int read = source.Read(buffer, 0, toRead);
  14. destination.Write(buffer, 0, read);
  15. if (read != toRead)
  16. {
  17. return;
  18. }
  19. }
  20. }
  21. public static byte[] ReadAllBytes(this Stream source)
  22. {
  23. source.Position = 0;
  24. var bytes = new byte[source.Length];
  25. source.Read(bytes, 0, bytes.Length);
  26. return bytes;
  27. }
  28. }
  29. }