|
3 | 3 |
|
4 | 4 | namespace HotChocolate.Utilities
|
5 | 5 | {
|
6 |
| - internal sealed class ArrayWriter |
7 |
| - : IBufferWriter<byte> |
8 |
| - , IDisposable |
| 6 | + /// <summary> |
| 7 | + /// A <see cref="IBufferWriter{T}"/> that writes to a rented buffer. |
| 8 | + /// </summary> |
| 9 | + internal sealed class ArrayWriter : IBufferWriter<byte>, IDisposable |
9 | 10 | {
|
10 | 11 | private const int _initialBufferSize = 512;
|
11 | 12 | private byte[] _buffer;
|
12 | 13 | private int _capacity;
|
13 | 14 | private int _start;
|
14 | 15 | private bool _disposed;
|
15 |
| - |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Initializes a new instance of the <see cref="ArrayWriter"/> class. |
| 19 | + /// </summary> |
16 | 20 | public ArrayWriter()
|
17 | 21 | {
|
18 | 22 | _buffer = ArrayPool<byte>.Shared.Rent(_initialBufferSize);
|
19 | 23 | _capacity = _buffer.Length;
|
20 | 24 | _start = 0;
|
21 | 25 | }
|
22 | 26 |
|
| 27 | + /// <summary> |
| 28 | + /// Gets the number of bytes written to the buffer. |
| 29 | + /// </summary> |
23 | 30 | public int Length => _start;
|
24 | 31 |
|
| 32 | + /// <summary> |
| 33 | + /// Gets the underlying buffer. |
| 34 | + /// </summary> |
| 35 | + /// <returns> |
| 36 | + /// The underlying buffer. |
| 37 | + /// </returns> |
| 38 | + /// <remarks> |
| 39 | + /// Accessing the underlying buffer directly is not recommended. |
| 40 | + /// If possible use <see cref="GetWrittenMemory"/> or <see cref="GetWrittenSpan"/>. |
| 41 | + /// </remarks> |
| 42 | + public byte[] GetInternalBuffer() => _buffer; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the part of the buffer that has been written to. |
| 46 | + /// </summary> |
| 47 | + /// <returns> |
| 48 | + /// A <see cref="ReadOnlyMemory{T}"/> of the written portion of the buffer. |
| 49 | + /// </returns> |
| 50 | + public ReadOnlyMemory<byte> GetWrittenMemory() |
| 51 | + => _buffer.AsMemory().Slice(0, _start); |
| 52 | + |
25 | 53 | public ReadOnlyMemory<byte> Body => _buffer.AsMemory().Slice(0, _start);
|
26 | 54 |
|
27 |
| - public byte[] GetInternalBuffer() => _buffer; |
| 55 | + /// <summary> |
| 56 | + /// Gets the part of the buffer that has been written to. |
| 57 | + /// </summary> |
| 58 | + /// <returns> |
| 59 | + /// A <see cref="ReadOnlySpan{T}"/> of the written portion of the buffer. |
| 60 | + /// </returns> |
| 61 | + public ReadOnlySpan<byte> GetWrittenSpan() |
| 62 | + => _buffer.AsSpan().Slice(0, _start); |
28 | 63 |
|
| 64 | + /// <summary> |
| 65 | + /// Advances the writer by the specified number of bytes. |
| 66 | + /// </summary> |
| 67 | + /// <param name="count"> |
| 68 | + /// The number of bytes to advance the writer by. |
| 69 | + /// </param> |
| 70 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 71 | + /// Thrown if <paramref name="count"/> is negative or |
| 72 | + /// if <paramref name="count"/> is greater than the |
| 73 | + /// available capacity on the internal buffer. |
| 74 | + /// </exception> |
29 | 75 | public void Advance(int count)
|
30 | 76 | {
|
| 77 | + if (_disposed) |
| 78 | + { |
| 79 | + throw new ObjectDisposedException(nameof(ArrayWriter)); |
| 80 | + } |
| 81 | + |
| 82 | + if (count < 0) |
| 83 | + { |
| 84 | + throw new ArgumentOutOfRangeException(nameof(count)); |
| 85 | + } |
| 86 | + |
| 87 | + if (count > _capacity) |
| 88 | + { |
| 89 | + throw new ArgumentOutOfRangeException( |
| 90 | + nameof(count), |
| 91 | + count, |
| 92 | + "ArrayWriter_Advance_BufferOverflow"); |
| 93 | + } |
| 94 | + |
31 | 95 | _start += count;
|
32 | 96 | _capacity -= count;
|
33 | 97 | }
|
34 | 98 |
|
| 99 | + /// <summary> |
| 100 | + /// Gets a <see cref="Memory{T}"/> to write to. |
| 101 | + /// </summary> |
| 102 | + /// <param name="sizeHint"> |
| 103 | + /// The minimum size of the returned <see cref="Memory{T}"/>. |
| 104 | + /// </param> |
| 105 | + /// <returns> |
| 106 | + /// A <see cref="Memory{T}"/> to write to. |
| 107 | + /// </returns> |
| 108 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 109 | + /// Thrown if <paramref name="sizeHint"/> is negative. |
| 110 | + /// </exception> |
35 | 111 | public Memory<byte> GetMemory(int sizeHint = 0)
|
36 | 112 | {
|
37 |
| - var size = sizeHint < 1 ? _initialBufferSize : sizeHint; |
| 113 | + if (_disposed) |
| 114 | + { |
| 115 | + throw new ObjectDisposedException(nameof(ArrayWriter)); |
| 116 | + } |
| 117 | + |
| 118 | + if (sizeHint < 0) |
| 119 | + { |
| 120 | + throw new ArgumentOutOfRangeException(nameof(sizeHint)); |
| 121 | + } |
| 122 | + |
| 123 | + var size = sizeHint < 1 |
| 124 | + ? _initialBufferSize |
| 125 | + : sizeHint; |
38 | 126 | EnsureBufferCapacity(size);
|
39 | 127 | return _buffer.AsMemory().Slice(_start, size);
|
40 | 128 | }
|
41 | 129 |
|
| 130 | + /// <summary> |
| 131 | + /// Gets a <see cref="Span{T}"/> to write to. |
| 132 | + /// </summary> |
| 133 | + /// <param name="sizeHint"> |
| 134 | + /// The minimum size of the returned <see cref="Span{T}"/>. |
| 135 | + /// </param> |
| 136 | + /// <returns> |
| 137 | + /// A <see cref="Span{T}"/> to write to. |
| 138 | + /// </returns> |
| 139 | + /// <exception cref="ArgumentOutOfRangeException"> |
| 140 | + /// Thrown if <paramref name="sizeHint"/> is negative. |
| 141 | + /// </exception> |
42 | 142 | public Span<byte> GetSpan(int sizeHint = 0)
|
43 | 143 | {
|
44 |
| - var size = sizeHint < 1 ? _initialBufferSize : sizeHint; |
| 144 | + if (_disposed) |
| 145 | + { |
| 146 | + throw new ObjectDisposedException(nameof(ArrayWriter)); |
| 147 | + } |
| 148 | + |
| 149 | + if (sizeHint < 0) |
| 150 | + { |
| 151 | + throw new ArgumentOutOfRangeException(nameof(sizeHint)); |
| 152 | + } |
| 153 | + |
| 154 | + var size = sizeHint < 1 |
| 155 | + ? _initialBufferSize |
| 156 | + : sizeHint; |
45 | 157 | EnsureBufferCapacity(size);
|
46 | 158 | return _buffer.AsSpan().Slice(_start, size);
|
47 | 159 | }
|
48 | 160 |
|
| 161 | + /// <summary> |
| 162 | + /// Gets the buffer as an <see cref="ArraySegment{T}"/> |
| 163 | + /// </summary> |
| 164 | + /// <returns></returns> |
| 165 | + public ArraySegment<byte> ToArraySegment() => new(_buffer, 0, _start); |
| 166 | + |
| 167 | + /// <summary> |
| 168 | + /// Ensures that the internal buffer has the needed capacity. |
| 169 | + /// </summary> |
| 170 | + /// <param name="neededCapacity"> |
| 171 | + /// The needed capacity on the internal buffer. |
| 172 | + /// </param> |
49 | 173 | private void EnsureBufferCapacity(int neededCapacity)
|
50 | 174 | {
|
| 175 | + // check if we have enough capacity available on the buffer. |
51 | 176 | if (_capacity < neededCapacity)
|
52 | 177 | {
|
53 |
| - byte[] buffer = _buffer; |
| 178 | + // if we need to expand the buffer we first capture the original buffer. |
| 179 | + var buffer = _buffer; |
| 180 | + |
| 181 | + // next we determine the new size of the buffer, we at least double the size to avoid |
| 182 | + // expanding the buffer too often. |
| 183 | + var newSize = buffer.Length * 2; |
54 | 184 |
|
55 |
| - int newSize = buffer.Length * 2; |
56 |
| - if (neededCapacity > buffer.Length) |
| 185 | + // if that new buffer size is not enough to satisfy the needed capacity |
| 186 | + // we add the needed capacity to the doubled buffer capacity. |
| 187 | + if (neededCapacity > newSize - _start) |
57 | 188 | {
|
58 | 189 | newSize += neededCapacity;
|
59 | 190 | }
|
60 | 191 |
|
| 192 | + // next we will rent a new array from the array pool that supports |
| 193 | + // the new capacity requirements. |
61 | 194 | _buffer = ArrayPool<byte>.Shared.Rent(newSize);
|
| 195 | + |
| 196 | + // the rented array might have a larger size than the needed capacity, |
| 197 | + // so we will take the buffer length and calculate from that the free capacity. |
62 | 198 | _capacity += _buffer.Length - buffer.Length;
|
63 | 199 |
|
| 200 | + // finally we copy the data from the original buffer to the new buffer. |
64 | 201 | buffer.AsSpan().CopyTo(_buffer);
|
| 202 | + |
| 203 | + // last but not least we return the original buffer to the array pool. |
65 | 204 | ArrayPool<byte>.Shared.Return(buffer);
|
66 | 205 | }
|
67 | 206 | }
|
68 | 207 |
|
69 | 208 | public void Clear()
|
70 | 209 | {
|
71 |
| - ArrayPool<byte>.Shared.Return(_buffer); |
72 |
| - _buffer = ArrayPool<byte>.Shared.Rent(_initialBufferSize); |
73 | 210 | _capacity = _buffer.Length;
|
74 | 211 | _start = 0;
|
75 | 212 | }
|
76 | 213 |
|
| 214 | + /// <inheritdoc/> |
77 | 215 | public void Dispose()
|
78 | 216 | {
|
79 | 217 | if (!_disposed)
|
80 | 218 | {
|
81 | 219 | ArrayPool<byte>.Shared.Return(_buffer);
|
82 | 220 | _buffer = Array.Empty<byte>();
|
| 221 | + _capacity = 0; |
| 222 | + _start = 0; |
83 | 223 | _disposed = true;
|
84 | 224 | }
|
85 | 225 | }
|
|
0 commit comments