PipedInputStream.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Threading;
  3. namespace SharpCifs.Util.Sharpen
  4. {
  5. internal class PipedInputStream : InputStream
  6. {
  7. private byte[] _oneBuffer;
  8. public const int PipeSize = 1024;
  9. protected byte[] Buffer;
  10. private bool _closed;
  11. private ManualResetEvent _dataEvent;
  12. private int _end;
  13. private int _start;
  14. private object _thisLock;
  15. private bool _allowGrow = false;
  16. public int In
  17. {
  18. get { return _start; }
  19. set { _start = value; }
  20. }
  21. public int Out
  22. {
  23. get { return _end; }
  24. set { _end = value; }
  25. }
  26. public PipedInputStream()
  27. {
  28. _thisLock = new object();
  29. _dataEvent = new ManualResetEvent(false);
  30. Buffer = new byte[PipeSize + 1];
  31. }
  32. public PipedInputStream(PipedOutputStream os) : this()
  33. {
  34. os.Attach(this);
  35. }
  36. public override void Close()
  37. {
  38. lock (_thisLock)
  39. {
  40. _closed = true;
  41. _dataEvent.Set();
  42. }
  43. }
  44. public override int Available()
  45. {
  46. lock (_thisLock)
  47. {
  48. if (_start <= _end)
  49. {
  50. return (_end - _start);
  51. }
  52. return ((Buffer.Length - _start) + _end);
  53. }
  54. }
  55. public override int Read()
  56. {
  57. if (_oneBuffer == null)
  58. _oneBuffer = new byte[1];
  59. if (Read(_oneBuffer, 0, 1) == -1)
  60. return -1;
  61. return _oneBuffer[0];
  62. }
  63. public override int Read(byte[] b, int offset, int len)
  64. {
  65. int length = 0;
  66. do
  67. {
  68. _dataEvent.WaitOne();
  69. lock (_thisLock)
  70. {
  71. if (_closed && Available() == 0)
  72. {
  73. return -1;
  74. }
  75. if (_start < _end)
  76. {
  77. length = Math.Min(len, _end - _start);
  78. Array.Copy(Buffer, _start, b, offset, length);
  79. _start += length;
  80. }
  81. else if (_start > _end)
  82. {
  83. length = Math.Min(len, Buffer.Length - _start);
  84. Array.Copy(Buffer, _start, b, offset, length);
  85. len -= length;
  86. _start = (_start + length) % Buffer.Length;
  87. if (len > 0)
  88. {
  89. int i = Math.Min(len, _end);
  90. Array.Copy(Buffer, 0, b, offset + length, i);
  91. _start += i;
  92. length += i;
  93. }
  94. }
  95. if (_start == _end && !_closed)
  96. {
  97. _dataEvent.Reset();
  98. }
  99. Monitor.PulseAll(_thisLock);
  100. }
  101. } while (length == 0);
  102. return length;
  103. }
  104. private int Allocate(int len)
  105. {
  106. int alen;
  107. while ((alen = TryAllocate(len)) == 0)
  108. {
  109. // Wait until somebody reads data
  110. try
  111. {
  112. Monitor.Wait(_thisLock);
  113. }
  114. catch
  115. {
  116. _closed = true;
  117. _dataEvent.Set();
  118. throw;
  119. }
  120. }
  121. return alen;
  122. }
  123. int TryAllocate(int len)
  124. {
  125. int free;
  126. if (_start <= _end)
  127. {
  128. free = (Buffer.Length - _end) + _start;
  129. }
  130. else
  131. {
  132. free = _start - _end;
  133. }
  134. if (free <= len)
  135. {
  136. if (!_allowGrow)
  137. return free > 0 ? free - 1 : 0;
  138. int sizeInc = (len - free) + 1;
  139. byte[] destinationArray = new byte[Buffer.Length + sizeInc];
  140. if (_start <= _end)
  141. {
  142. Array.Copy(Buffer, _start, destinationArray, _start, _end - _start);
  143. }
  144. else
  145. {
  146. Array.Copy(Buffer,
  147. 0,
  148. destinationArray,
  149. 0,
  150. _end);
  151. Array.Copy(Buffer,
  152. _start,
  153. destinationArray,
  154. _start + sizeInc,
  155. Buffer.Length - _start);
  156. _start += sizeInc;
  157. }
  158. Buffer = destinationArray;
  159. }
  160. return len;
  161. }
  162. internal void Write(int b)
  163. {
  164. lock (_thisLock)
  165. {
  166. Allocate(1);
  167. Buffer[_end] = (byte)b;
  168. _end = (_end + 1) % Buffer.Length;
  169. _dataEvent.Set();
  170. }
  171. }
  172. internal void Write(byte[] b, int offset, int len)
  173. {
  174. do
  175. {
  176. lock (_thisLock)
  177. {
  178. int alen = Allocate(len);
  179. int length = Math.Min(Buffer.Length - _end, alen);
  180. Array.Copy(b, offset, Buffer, _end, length);
  181. _end = (_end + length) % Buffer.Length;
  182. if (length < alen)
  183. {
  184. Array.Copy(b, offset + length, Buffer, 0, alen - length);
  185. _end += alen - length;
  186. }
  187. _dataEvent.Set();
  188. len -= alen;
  189. offset += alen;
  190. }
  191. } while (len > 0);
  192. }
  193. }
  194. }