HttpAbstractions 277 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. commit e80d0b000ac6b9a32ddaf01dc89b2c0b9a1d0523
  2. Author: Tornhoof <[email protected]>
  3. Date: Tue Mar 27 01:03:16 2018 +0200
  4. Make BufferedReadString public and add doc (#1011)
  5. diff --git a/src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs b/src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs
  6. index b72920df4d8..10f1465f3a2 100644
  7. --- a/src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs
  8. +++ b/src/Microsoft.AspNetCore.WebUtilities/BufferedReadStream.cs
  9. @@ -10,7 +10,11 @@ using System.Threading.Tasks;
  10. namespace Microsoft.AspNetCore.WebUtilities
  11. {
  12. - internal class BufferedReadStream : Stream
  13. + /// <summary>
  14. + /// A Stream that wraps another stream and allows reading lines.
  15. + /// The data is buffered in memory.
  16. + /// </summary>
  17. + public class BufferedReadStream : Stream
  18. {
  19. private const byte CR = (byte)'\r';
  20. private const byte LF = (byte)'\n';
  21. @@ -22,11 +26,22 @@ namespace Microsoft.AspNetCore.WebUtilities
  22. private int _bufferCount = 0;
  23. private bool _disposed;
  24. + /// <summary>
  25. + /// Creates a new stream.
  26. + /// </summary>
  27. + /// <param name="inner">The stream to wrap.</param>
  28. + /// <param name="bufferSize">Size of buffer in bytes.</param>
  29. public BufferedReadStream(Stream inner, int bufferSize)
  30. : this(inner, bufferSize, ArrayPool<byte>.Shared)
  31. {
  32. }
  33. + /// <summary>
  34. + /// Creates a new stream.
  35. + /// </summary>
  36. + /// <param name="inner">The stream to wrap.</param>
  37. + /// <param name="bufferSize">Size of buffer in bytes.</param>
  38. + /// <param name="bytePool">ArrayPool for the buffer.</param>
  39. public BufferedReadStream(Stream inner, int bufferSize, ArrayPool<byte> bytePool)
  40. {
  41. if (inner == null)
  42. @@ -39,36 +54,45 @@ namespace Microsoft.AspNetCore.WebUtilities
  43. _buffer = bytePool.Rent(bufferSize);
  44. }
  45. + /// <summary>
  46. + /// The currently buffered data.
  47. + /// </summary>
  48. public ArraySegment<byte> BufferedData
  49. {
  50. get { return new ArraySegment<byte>(_buffer, _bufferOffset, _bufferCount); }
  51. }
  52. + /// <inheritdoc/>
  53. public override bool CanRead
  54. {
  55. get { return _inner.CanRead || _bufferCount > 0; }
  56. }
  57. + /// <inheritdoc/>
  58. public override bool CanSeek
  59. {
  60. get { return _inner.CanSeek; }
  61. }
  62. + /// <inheritdoc/>
  63. public override bool CanTimeout
  64. {
  65. get { return _inner.CanTimeout; }
  66. }
  67. + /// <inheritdoc/>
  68. public override bool CanWrite
  69. {
  70. get { return _inner.CanWrite; }
  71. }
  72. + /// <inheritdoc/>
  73. public override long Length
  74. {
  75. get { return _inner.Length; }
  76. }
  77. + /// <inheritdoc/>
  78. public override long Position
  79. {
  80. get { return _inner.Position - _bufferCount; }
  81. @@ -112,6 +136,7 @@ namespace Microsoft.AspNetCore.WebUtilities
  82. }
  83. }
  84. + /// <inheritdoc/>
  85. public override long Seek(long offset, SeekOrigin origin)
  86. {
  87. if (origin == SeekOrigin.Begin)
  88. @@ -129,11 +154,13 @@ namespace Microsoft.AspNetCore.WebUtilities
  89. return Position;
  90. }
  91. + /// <inheritdoc/>
  92. public override void SetLength(long value)
  93. {
  94. _inner.SetLength(value);
  95. }
  96. + /// <inheritdoc/>
  97. protected override void Dispose(bool disposing)
  98. {
  99. if (!_disposed)
  100. @@ -148,26 +175,31 @@ namespace Microsoft.AspNetCore.WebUtilities
  101. }
  102. }
  103. + /// <inheritdoc/>
  104. public override void Flush()
  105. {
  106. _inner.Flush();
  107. }
  108. + /// <inheritdoc/>
  109. public override Task FlushAsync(CancellationToken cancellationToken)
  110. {
  111. return _inner.FlushAsync(cancellationToken);
  112. }
  113. + /// <inheritdoc/>
  114. public override void Write(byte[] buffer, int offset, int count)
  115. {
  116. _inner.Write(buffer, offset, count);
  117. }
  118. + /// <inheritdoc/>
  119. public override Task WriteAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  120. {
  121. return _inner.WriteAsync(buffer, offset, count, cancellationToken);
  122. }
  123. + /// <inheritdoc/>
  124. public override int Read(byte[] buffer, int offset, int count)
  125. {
  126. ValidateBuffer(buffer, offset, count);
  127. @@ -185,6 +217,7 @@ namespace Microsoft.AspNetCore.WebUtilities
  128. return _inner.Read(buffer, offset, count);
  129. }
  130. + /// <inheritdoc/>
  131. public async override Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
  132. {
  133. ValidateBuffer(buffer, offset, count);
  134. @@ -202,6 +235,10 @@ namespace Microsoft.AspNetCore.WebUtilities
  135. return await _inner.ReadAsync(buffer, offset, count, cancellationToken);
  136. }
  137. + /// <summary>
  138. + /// Ensures that the buffer is not empty.
  139. + /// </summary>
  140. + /// <returns>Returns <c>true</c> if the buffer is not empty; <c>false</c> otherwise.</returns>
  141. public bool EnsureBuffered()
  142. {
  143. if (_bufferCount > 0)
  144. @@ -214,6 +251,11 @@ namespace Microsoft.AspNetCore.WebUtilities
  145. return _bufferCount > 0;
  146. }
  147. + /// <summary>
  148. + /// Ensures that the buffer is not empty.
  149. + /// </summary>
  150. + /// <param name="cancellationToken">Cancellation token.</param>
  151. + /// <returns>Returns <c>true</c> if the buffer is not empty; <c>false</c> otherwise.</returns>
  152. public async Task<bool> EnsureBufferedAsync(CancellationToken cancellationToken)
  153. {
  154. if (_bufferCount > 0)
  155. @@ -226,6 +268,11 @@ namespace Microsoft.AspNetCore.WebUtilities
  156. return _bufferCount > 0;
  157. }
  158. + /// <summary>
  159. + /// Ensures that a minimum amount of buffered data is available.
  160. + /// </summary>
  161. + /// <param name="minCount">Minimum amount of buffered data.</param>
  162. + /// <returns>Returns <c>true</c> if the minimum amount of buffered data is available; <c>false</c> otherwise.</returns>
  163. public bool EnsureBuffered(int minCount)
  164. {
  165. if (minCount > _buffer.Length)
  166. @@ -253,6 +300,12 @@ namespace Microsoft.AspNetCore.WebUtilities
  167. return true;
  168. }
  169. + /// <summary>
  170. + /// Ensures that a minimum amount of buffered data is available.
  171. + /// </summary>
  172. + /// <param name="minCount">Minimum amount of buffered data.</param>
  173. + /// <param name="cancellationToken">Cancellation token.</param>
  174. + /// <returns>Returns <c>true</c> if the minimum amount of buffered data is available; <c>false</c> otherwise.</returns>
  175. public async Task<bool> EnsureBufferedAsync(int minCount, CancellationToken cancellationToken)
  176. {
  177. if (minCount > _buffer.Length)
  178. @@ -280,6 +333,13 @@ namespace Microsoft.AspNetCore.WebUtilities
  179. return true;
  180. }
  181. + /// <summary>
  182. + /// Reads a line. A line is defined as a sequence of characters followed by
  183. + /// a carriage return immediately followed by a line feed. The resulting string does not
  184. + /// contain the terminating carriage return and line feed.
  185. + /// </summary>
  186. + /// <param name="lengthLimit">Maximum allowed line length.</param>
  187. + /// <returns>A line.</returns>
  188. public string ReadLine(int lengthLimit)
  189. {
  190. CheckDisposed();
  191. @@ -300,6 +360,14 @@ namespace Microsoft.AspNetCore.WebUtilities
  192. }
  193. }
  194. + /// <summary>
  195. + /// Reads a line. A line is defined as a sequence of characters followed by
  196. + /// a carriage return immediately followed by a line feed. The resulting string does not
  197. + /// contain the terminating carriage return and line feed.
  198. + /// </summary>
  199. + /// <param name="lengthLimit">Maximum allowed line length.</param>
  200. + /// <param name="cancellationToken">Cancellation token.</param>
  201. + /// <returns>A line.</returns>
  202. public async Task<string> ReadLineAsync(int lengthLimit, CancellationToken cancellationToken)
  203. {
  204. CheckDisposed();