PipedInputStream.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. get { return _start; }
  18. set { _start = value; }
  19. }
  20. public int Out {
  21. get { return _end; }
  22. set { _end = value; }
  23. }
  24. public PipedInputStream ()
  25. {
  26. _thisLock = new object ();
  27. _dataEvent = new ManualResetEvent (false);
  28. Buffer = new byte[PipeSize + 1];
  29. }
  30. public PipedInputStream (PipedOutputStream os): this ()
  31. {
  32. os.Attach (this);
  33. }
  34. public override void Close ()
  35. {
  36. lock (_thisLock) {
  37. _closed = true;
  38. _dataEvent.Set ();
  39. }
  40. }
  41. public override int Available ()
  42. {
  43. lock (_thisLock) {
  44. if (_start <= _end) {
  45. return (_end - _start);
  46. }
  47. return ((Buffer.Length - _start) + _end);
  48. }
  49. }
  50. public override int Read ()
  51. {
  52. if (_oneBuffer == null)
  53. _oneBuffer = new byte[1];
  54. if (Read (_oneBuffer, 0, 1) == -1)
  55. return -1;
  56. return _oneBuffer[0];
  57. }
  58. public override int Read (byte[] b, int offset, int len)
  59. {
  60. int length = 0;
  61. do {
  62. _dataEvent.WaitOne ();
  63. lock (_thisLock) {
  64. if (_closed && Available () == 0) {
  65. return -1;
  66. }
  67. if (_start < _end) {
  68. length = Math.Min (len, _end - _start);
  69. Array.Copy (Buffer, _start, b, offset, length);
  70. _start += length;
  71. } else if (_start > _end) {
  72. length = Math.Min (len, Buffer.Length - _start);
  73. Array.Copy (Buffer, _start, b, offset, length);
  74. len -= length;
  75. _start = (_start + length) % Buffer.Length;
  76. if (len > 0) {
  77. int i = Math.Min (len, _end);
  78. Array.Copy (Buffer, 0, b, offset + length, i);
  79. _start += i;
  80. length += i;
  81. }
  82. }
  83. if (_start == _end && !_closed) {
  84. _dataEvent.Reset ();
  85. }
  86. Monitor.PulseAll (_thisLock);
  87. }
  88. } while (length == 0);
  89. return length;
  90. }
  91. private int Allocate (int len)
  92. {
  93. int alen;
  94. while ((alen = TryAllocate (len)) == 0) {
  95. // Wait until somebody reads data
  96. try {
  97. Monitor.Wait (_thisLock);
  98. } catch {
  99. _closed = true;
  100. _dataEvent.Set ();
  101. throw;
  102. }
  103. }
  104. return alen;
  105. }
  106. int TryAllocate (int len)
  107. {
  108. int free;
  109. if (_start <= _end) {
  110. free = (Buffer.Length - _end) + _start;
  111. } else {
  112. free = _start - _end;
  113. }
  114. if (free <= len) {
  115. if (!_allowGrow)
  116. return free > 0 ? free - 1 : 0;
  117. int sizeInc = (len - free) + 1;
  118. byte[] destinationArray = new byte[Buffer.Length + sizeInc];
  119. if (_start <= _end) {
  120. Array.Copy (Buffer, _start, destinationArray, _start, _end - _start);
  121. } else {
  122. Array.Copy (Buffer, 0, destinationArray, 0, _end);
  123. Array.Copy (Buffer, _start, destinationArray, _start + sizeInc, Buffer.Length - _start);
  124. _start += sizeInc;
  125. }
  126. Buffer = destinationArray;
  127. }
  128. return len;
  129. }
  130. internal void Write (int b)
  131. {
  132. lock (_thisLock) {
  133. Allocate (1);
  134. Buffer[_end] = (byte)b;
  135. _end = (_end + 1) % Buffer.Length;
  136. _dataEvent.Set ();
  137. }
  138. }
  139. internal void Write (byte[] b, int offset, int len)
  140. {
  141. do {
  142. lock (_thisLock) {
  143. int alen = Allocate (len);
  144. int length = Math.Min (Buffer.Length - _end, alen);
  145. Array.Copy (b, offset, Buffer, _end, length);
  146. _end = (_end + length) % Buffer.Length;
  147. if (length < alen) {
  148. Array.Copy (b, offset + length, Buffer, 0, alen - length);
  149. _end += alen - length;
  150. }
  151. _dataEvent.Set ();
  152. len -= alen;
  153. offset += alen;
  154. }
  155. } while (len > 0);
  156. }
  157. }
  158. }