UploadStream.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. using BestHTTP;
  2. using System;
  3. using System.IO;
  4. using System.Threading;
  5. public sealed class UploadStream : Stream
  6. {
  7. #region Private Fields
  8. /// <summary>
  9. /// Buffer for reads
  10. /// </summary>
  11. MemoryStream ReadBuffer = new MemoryStream();
  12. /// <summary>
  13. /// Buffer for writes
  14. /// </summary>
  15. MemoryStream WriteBuffer = new MemoryStream();
  16. /// <summary>
  17. /// Indicates that we will not write more data to this stream
  18. /// </summary>
  19. bool noMoreData;
  20. /// <summary>
  21. /// For thread synchronization
  22. /// </summary>
  23. AutoResetEvent ARE = new AutoResetEvent(false);
  24. /// <summary>
  25. /// For thread synchronization
  26. /// </summary>
  27. object locker = new object();
  28. #endregion
  29. #region Properties
  30. /// <summary>
  31. /// Name of this stream for easier debugging
  32. /// </summary>
  33. public string Name { get; private set; }
  34. /// <summary>
  35. /// true if we are read all data from the read buffer
  36. /// </summary>
  37. private bool IsReadBufferEmpty { get { lock (locker) return ReadBuffer.Position == ReadBuffer.Length; } }
  38. #endregion
  39. #region Constructors
  40. public UploadStream(string name)
  41. : this()
  42. {
  43. this.Name = name;
  44. }
  45. public UploadStream()
  46. {
  47. this.ReadBuffer = new MemoryStream();
  48. this.WriteBuffer = new MemoryStream();
  49. this.Name = string.Empty;
  50. }
  51. #endregion
  52. #region Stream Implementation
  53. public override int Read(byte[] buffer, int offset, int count)
  54. {
  55. // We will not push more data to the write buffer
  56. if (noMoreData)
  57. {
  58. // No data left in the read buffer
  59. if (ReadBuffer.Position == ReadBuffer.Length)
  60. {
  61. // Is there any data in the write buffer? If so, switch the buffers
  62. if (WriteBuffer.Length > 0)
  63. SwitchBuffers();
  64. else
  65. {
  66. HTTPManager.Logger.Information("UploadStream", string.Format("{0} - Read - End Of Stream", this.Name));
  67. return -1;
  68. }
  69. }
  70. else
  71. return ReadBuffer.Read(buffer, offset, count);
  72. }
  73. // There are no more data in the read buffer? Wait for it.
  74. if (IsReadBufferEmpty)
  75. {
  76. ARE.WaitOne();
  77. lock (locker)
  78. if (IsReadBufferEmpty && WriteBuffer.Length > 0)
  79. SwitchBuffers();
  80. }
  81. int read = -1;
  82. lock (locker)
  83. read = ReadBuffer.Read(buffer, offset, count);
  84. return read;
  85. }
  86. public override void Write(byte[] buffer, int offset, int count)
  87. {
  88. if (noMoreData)
  89. throw new System.ArgumentException("noMoreData already set!");
  90. lock (locker)
  91. {
  92. WriteBuffer.Write(buffer, offset, count);
  93. SwitchBuffers();
  94. }
  95. ARE.Set();
  96. }
  97. public override void Flush()
  98. {
  99. Finish();
  100. }
  101. #endregion
  102. #region Dispose Implementation
  103. protected override void Dispose(bool disposing)
  104. {
  105. if (disposing)
  106. {
  107. HTTPManager.Logger.Information("UploadStream", string.Format("{0} - Dispose", this.Name));
  108. ReadBuffer.Dispose();
  109. ReadBuffer = null;
  110. WriteBuffer.Dispose();
  111. WriteBuffer = null;
  112. #if NETFX_CORE
  113. ARE.Dispose();
  114. #else
  115. ARE.Close();
  116. #endif
  117. ARE = null;
  118. }
  119. base.Dispose(disposing);
  120. }
  121. #endregion
  122. #region Helper Functions
  123. public void Finish()
  124. {
  125. if (noMoreData)
  126. throw new System.ArgumentException("noMoreData already set!");
  127. HTTPManager.Logger.Information("UploadStream", string.Format("{0} - Finish", this.Name));
  128. noMoreData = true;
  129. ARE.Set();
  130. }
  131. private bool SwitchBuffers()
  132. {
  133. // Switch the buffers only when all data are consumed from our read buffer
  134. lock (locker)
  135. {
  136. if (ReadBuffer.Position == ReadBuffer.Length)
  137. {
  138. // This buffer will be the read buffer, we need to seek back to the beginning
  139. WriteBuffer.Seek(0, SeekOrigin.Begin);
  140. // This will be the write buffer, set the length to zero
  141. ReadBuffer.SetLength(0);
  142. // switch the two buffers
  143. MemoryStream tmp = WriteBuffer;
  144. WriteBuffer = ReadBuffer;
  145. ReadBuffer = tmp;
  146. return true;
  147. }
  148. }
  149. return false;
  150. }
  151. #endregion
  152. #region Not Implemented Functions and Properties
  153. public override bool CanRead { get { throw new NotImplementedException(); } }
  154. public override bool CanSeek { get { throw new NotImplementedException(); } }
  155. public override bool CanWrite { get { throw new NotImplementedException(); } }
  156. public override long Length { get { throw new NotImplementedException(); } }
  157. public override long Position { get { throw new NotImplementedException(); } set { throw new NotImplementedException(); } }
  158. public override long Seek(long offset, SeekOrigin origin)
  159. {
  160. throw new NotImplementedException();
  161. }
  162. public override void SetLength(long value)
  163. {
  164. throw new NotImplementedException();
  165. }
  166. #endregion
  167. }