SiphonStream.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.IO;
  3. namespace PaintDotNet
  4. {
  5. /// <summary>
  6. /// This was written as a workaround for a bug in SharpZipLib that prevents it
  7. /// from working right with huge Write() commands. So we split the incoming
  8. /// requests into smaller requests, like 4KB each or so.
  9. ///
  10. /// However, this didn't work around the bug. But now I use this class so that
  11. /// I can keep tabs on a serialization or deserialization operation and have a
  12. /// dialog box with a progress bar.
  13. /// </summary>
  14. public sealed class SiphonStream
  15. : Stream
  16. {
  17. private Exception throwMe;
  18. private Stream stream;
  19. private int siphonSize;
  20. private object tag = null;
  21. public object Tag
  22. {
  23. get
  24. {
  25. return this.tag;
  26. }
  27. set
  28. {
  29. this.tag = value;
  30. }
  31. }
  32. /// <summary>
  33. /// Causes the next call to Read() or Write() to throw an IOException instead. The
  34. /// exception passed to this method will be used as the InnerException.
  35. /// </summary>
  36. /// <param name="throwMe"></param>
  37. public void Abort(Exception newThrowMe)
  38. {
  39. if (newThrowMe == null)
  40. {
  41. throw new ArgumentException("throwMe may not be null", "throwMe");
  42. }
  43. this.throwMe = newThrowMe;
  44. }
  45. public event IOEventHandler IOFinished;
  46. private void OnIOFinished(IOEventArgs e)
  47. {
  48. if (IOFinished != null)
  49. {
  50. IOFinished(this, e);
  51. }
  52. }
  53. int readAccumulator = 0;
  54. int writeAccumulator = 0;
  55. private void ReadAccumulate(int count)
  56. {
  57. if (count == -1)
  58. {
  59. if (this.readAccumulator > 0)
  60. {
  61. OnIOFinished(new IOEventArgs(IOOperationType.Read, this.Position, this.readAccumulator));
  62. this.readAccumulator = 0;
  63. }
  64. }
  65. else
  66. {
  67. WriteAccumulate(-1);
  68. this.readAccumulator += count;
  69. while (this.readAccumulator > this.siphonSize)
  70. {
  71. OnIOFinished(new IOEventArgs(IOOperationType.Read, this.Position - this.readAccumulator + this.siphonSize, this.siphonSize));
  72. this.readAccumulator -= this.siphonSize;
  73. }
  74. }
  75. }
  76. private void WriteAccumulate(int count)
  77. {
  78. if (count == -1)
  79. {
  80. if (this.writeAccumulator > 0)
  81. {
  82. OnIOFinished(new IOEventArgs(IOOperationType.Write, this.Position, writeAccumulator));
  83. this.writeAccumulator = 0;
  84. }
  85. }
  86. else
  87. {
  88. ReadAccumulate(-1);
  89. this.writeAccumulator += count;
  90. while (this.writeAccumulator > this.siphonSize)
  91. {
  92. OnIOFinished(new IOEventArgs(IOOperationType.Write, this.Position - this.writeAccumulator + this.siphonSize, this.siphonSize));
  93. this.writeAccumulator -= this.siphonSize;
  94. }
  95. }
  96. }
  97. public override int Read(byte[] buffer, int offset, int count)
  98. {
  99. if (throwMe != null)
  100. {
  101. throw new IOException("Aborted", this.throwMe);
  102. }
  103. int countLeft = count;
  104. int cursor = offset;
  105. int totalAmountRead = 0;
  106. while (cursor < offset + count)
  107. {
  108. int count2 = Math.Min(this.siphonSize, countLeft);
  109. int amountRead = stream.Read(buffer, cursor, count2);
  110. ReadAccumulate(amountRead);
  111. countLeft -= amountRead;
  112. cursor += amountRead;
  113. totalAmountRead += amountRead;
  114. if (amountRead == 0)
  115. {
  116. break;
  117. }
  118. }
  119. return totalAmountRead;
  120. }
  121. public override void Write(byte[] buffer, int offset, int count)
  122. {
  123. if (this.throwMe != null)
  124. {
  125. throw new IOException("Aborted", this.throwMe);
  126. }
  127. int countLeft = count;
  128. int cursor = offset;
  129. while (cursor < offset + count)
  130. {
  131. int count2 = Math.Min(this.siphonSize, countLeft);
  132. stream.Write(buffer, cursor, count2);
  133. WriteAccumulate(count2);
  134. countLeft -= count2;
  135. cursor += count2;
  136. }
  137. }
  138. public override bool CanRead
  139. {
  140. get
  141. {
  142. return this.stream.CanRead;
  143. }
  144. }
  145. public override bool CanWrite
  146. {
  147. get
  148. {
  149. return this.stream.CanWrite;
  150. }
  151. }
  152. public override bool CanSeek
  153. {
  154. get
  155. {
  156. return this.stream.CanSeek;
  157. }
  158. }
  159. public override void Flush()
  160. {
  161. this.stream.Flush();
  162. }
  163. public override long Length
  164. {
  165. get
  166. {
  167. return this.stream.Length;
  168. }
  169. }
  170. public override long Position
  171. {
  172. get
  173. {
  174. return this.stream.Position;
  175. }
  176. set
  177. {
  178. this.stream.Position = value;
  179. }
  180. }
  181. public override long Seek(long offset, SeekOrigin origin)
  182. {
  183. return this.stream.Seek(offset, origin);
  184. }
  185. public override void SetLength(long value)
  186. {
  187. this.stream.SetLength(value);
  188. }
  189. public SiphonStream(Stream underlyingStream)
  190. : this(underlyingStream, 65536)
  191. {
  192. }
  193. public SiphonStream(Stream underlyingStream, int siphonSize)
  194. {
  195. this.stream = underlyingStream;
  196. this.siphonSize = siphonSize;
  197. }
  198. }
  199. }