StreamHelper.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.IO;
  3. using System.Threading;
  4. namespace LJLib.Tools.Helper
  5. {
  6. public class StreamHelper
  7. {
  8. public static void StreamCopy(Stream dest, Stream source, int len)
  9. {
  10. byte[] buff = new byte[10240];
  11. int total = 0;
  12. while (total < len)
  13. {
  14. var read = source.Read(buff, 0, Math.Min(buff.Length, len - total));
  15. if (read > 0)
  16. {
  17. dest.Write(buff, 0, read);
  18. dest.Flush();
  19. total += read;
  20. }
  21. else
  22. {
  23. Thread.Sleep(100);
  24. }
  25. }
  26. }
  27. public static void StreamCopy(Stream dest, Stream source)
  28. {
  29. byte[] buff = new byte[10240];
  30. int read = 0;
  31. int total = 0;
  32. while ((read = source.Read(buff, 0, buff.Length)) > 0)
  33. {
  34. dest.Write(buff, 0, read);
  35. dest.Flush();
  36. total += read;
  37. }
  38. }
  39. }
  40. }