StreamHelper.cs 1.3 KB

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