Buffer.cs 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. using System;
  2. using System.Diagnostics;
  3. using System.Text;
  4. namespace NetCoreServer
  5. {
  6. /// <summary>
  7. /// Dynamic byte buffer
  8. /// </summary>
  9. public class Buffer
  10. {
  11. private byte[] _data;
  12. private long _size;
  13. private long _offset;
  14. /// <summary>
  15. /// Is the buffer empty?
  16. /// </summary>
  17. public bool IsEmpty => (_data == null) || (_size == 0);
  18. /// <summary>
  19. /// Bytes memory buffer
  20. /// </summary>
  21. public byte[] Data => _data;
  22. /// <summary>
  23. /// Bytes memory buffer capacity
  24. /// </summary>
  25. public long Capacity => _data.Length;
  26. /// <summary>
  27. /// Bytes memory buffer size
  28. /// </summary>
  29. public long Size => _size;
  30. /// <summary>
  31. /// Bytes memory buffer offset
  32. /// </summary>
  33. public long Offset => _offset;
  34. /// <summary>
  35. /// Buffer indexer operator
  36. /// </summary>
  37. public byte this[long index] => _data[index];
  38. /// <summary>
  39. /// Initialize a new expandable buffer with zero capacity
  40. /// </summary>
  41. public Buffer() { _data = new byte[0]; _size = 0; _offset = 0; }
  42. /// <summary>
  43. /// Initialize a new expandable buffer with the given capacity
  44. /// </summary>
  45. public Buffer(long capacity) { _data = new byte[capacity]; _size = 0; _offset = 0; }
  46. /// <summary>
  47. /// Initialize a new expandable buffer with the given data
  48. /// </summary>
  49. public Buffer(byte[] data) { _data = data; _size = data.Length; _offset = 0; }
  50. #region Memory buffer methods
  51. /// <summary>
  52. /// Get a span of bytes from the current buffer
  53. /// </summary>
  54. public Span<byte> AsSpan()
  55. {
  56. return new Span<byte>(_data, (int)_offset, (int)_size);
  57. }
  58. /// <summary>
  59. /// Get a string from the current buffer
  60. /// </summary>
  61. public override string ToString()
  62. {
  63. return ExtractString(0, _size);
  64. }
  65. /// <summary>
  66. /// Clear the current buffer and its offset
  67. /// </summary>
  68. public void Clear()
  69. {
  70. _size = 0;
  71. _offset = 0;
  72. }
  73. /// <summary>
  74. /// Extract the string from buffer of the given offset and size
  75. /// </summary>
  76. public string ExtractString(long offset, long size)
  77. {
  78. Debug.Assert(((offset + size) <= Size), "Invalid offset & size!");
  79. if ((offset + size) > Size)
  80. throw new ArgumentException("Invalid offset & size!", nameof(offset));
  81. return Encoding.UTF8.GetString(_data, (int)offset, (int)size);
  82. }
  83. /// <summary>
  84. /// Remove the buffer of the given offset and size
  85. /// </summary>
  86. public void Remove(long offset, long size)
  87. {
  88. Debug.Assert(((offset + size) <= Size), "Invalid offset & size!");
  89. if ((offset + size) > Size)
  90. throw new ArgumentException("Invalid offset & size!", nameof(offset));
  91. Array.Copy(_data, offset + size, _data, offset, _size - size - offset);
  92. _size -= size;
  93. if (_offset >= (offset + size))
  94. _offset -= size;
  95. else if (_offset >= offset)
  96. {
  97. _offset -= _offset - offset;
  98. if (_offset > Size)
  99. _offset = Size;
  100. }
  101. }
  102. /// <summary>
  103. /// Reserve the buffer of the given capacity
  104. /// </summary>
  105. public void Reserve(long capacity)
  106. {
  107. Debug.Assert((capacity >= 0), "Invalid reserve capacity!");
  108. if (capacity < 0)
  109. throw new ArgumentException("Invalid reserve capacity!", nameof(capacity));
  110. if (capacity > Capacity)
  111. {
  112. byte[] data = new byte[Math.Max(capacity, 2 * Capacity)];
  113. Array.Copy(_data, 0, data, 0, _size);
  114. _data = data;
  115. }
  116. }
  117. /// <summary>
  118. /// Resize the current buffer
  119. /// </summary>
  120. public void Resize(long size)
  121. {
  122. Reserve(size);
  123. _size = size;
  124. if (_offset > _size)
  125. _offset = _size;
  126. }
  127. /// <summary>
  128. /// Shift the current buffer offset
  129. /// </summary>
  130. public void Shift(long offset) { _offset += offset; }
  131. /// <summary>
  132. /// Unshift the current buffer offset
  133. /// </summary>
  134. public void Unshift(long offset) { _offset -= offset; }
  135. #endregion
  136. #region Buffer I/O methods
  137. /// <summary>
  138. /// Append the single byte
  139. /// </summary>
  140. /// <param name="value">Byte value to append</param>
  141. /// <returns>Count of append bytes</returns>
  142. public long Append(byte value)
  143. {
  144. Reserve(_size + 1);
  145. _data[_size] = value;
  146. _size += 1;
  147. return 1;
  148. }
  149. /// <summary>
  150. /// Append the given buffer
  151. /// </summary>
  152. /// <param name="buffer">Buffer to append</param>
  153. /// <returns>Count of append bytes</returns>
  154. public long Append(byte[] buffer)
  155. {
  156. Reserve(_size + buffer.Length);
  157. Array.Copy(buffer, 0, _data, _size, buffer.Length);
  158. _size += buffer.Length;
  159. return buffer.Length;
  160. }
  161. /// <summary>
  162. /// Append the given buffer fragment
  163. /// </summary>
  164. /// <param name="buffer">Buffer to append</param>
  165. /// <param name="offset">Buffer offset</param>
  166. /// <param name="size">Buffer size</param>
  167. /// <returns>Count of append bytes</returns>
  168. public long Append(byte[] buffer, long offset, long size)
  169. {
  170. Reserve(_size + size);
  171. Array.Copy(buffer, offset, _data, _size, size);
  172. _size += size;
  173. return size;
  174. }
  175. /// <summary>
  176. /// Append the given span of bytes
  177. /// </summary>
  178. /// <param name="buffer">Buffer to append as a span of bytes</param>
  179. /// <returns>Count of append bytes</returns>
  180. public long Append(ReadOnlySpan<byte> buffer)
  181. {
  182. Reserve(_size + buffer.Length);
  183. buffer.CopyTo(new Span<byte>(_data, (int)_size, buffer.Length));
  184. _size += buffer.Length;
  185. return buffer.Length;
  186. }
  187. /// <summary>
  188. /// Append the given buffer
  189. /// </summary>
  190. /// <param name="buffer">Buffer to append</param>
  191. /// <returns>Count of append bytes</returns>
  192. public long Append(Buffer buffer) => Append(buffer.AsSpan());
  193. /// <summary>
  194. /// Append the given text in UTF-8 encoding
  195. /// </summary>
  196. /// <param name="text">Text to append</param>
  197. /// <returns>Count of append bytes</returns>
  198. public long Append(string text)
  199. {
  200. int length = Encoding.UTF8.GetMaxByteCount(text.Length);
  201. Reserve(_size + length);
  202. long result = Encoding.UTF8.GetBytes(text, 0, text.Length, _data, (int)_size);
  203. _size += result;
  204. return result;
  205. }
  206. /// <summary>
  207. /// Append the given text in UTF-8 encoding
  208. /// </summary>
  209. /// <param name="text">Text to append as a span of characters</param>
  210. /// <returns>Count of append bytes</returns>
  211. public long Append(ReadOnlySpan<char> text)
  212. {
  213. int length = Encoding.UTF8.GetMaxByteCount(text.Length);
  214. Reserve(_size + length);
  215. long result = Encoding.UTF8.GetBytes(text, new Span<byte>(_data, (int)_size, length));
  216. _size += result;
  217. return result;
  218. }
  219. #endregion
  220. }
  221. }