|
| 1 | +// We only need this if we aren't targeting .NET 6.0 or greater since it already exists there |
| 2 | +#if !NET6_0_OR_GREATER |
| 3 | +using System; |
| 4 | +using System.Diagnostics; |
| 5 | +using System.IO; |
| 6 | +using System.Threading; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace System.Buffers |
| 10 | +{ |
| 11 | + public class ArrayBufferWriter<T> : IBufferWriter<T>, IDisposable |
| 12 | + { |
| 13 | + private T[] _rentedBuffer; |
| 14 | + private int _written; |
| 15 | + private long _committed; |
| 16 | + |
| 17 | + private const int MinimumBufferSize = 256; |
| 18 | + |
| 19 | + public ArrayBufferWriter(int initialCapacity = MinimumBufferSize) |
| 20 | + { |
| 21 | + if (initialCapacity <= 0) |
| 22 | + { |
| 23 | + throw new ArgumentException(null, nameof(initialCapacity)); |
| 24 | + } |
| 25 | + |
| 26 | + _rentedBuffer = ArrayPool<T>.Shared.Rent(initialCapacity); |
| 27 | + _written = 0; |
| 28 | + _committed = 0; |
| 29 | + } |
| 30 | + |
| 31 | + public Memory<T> WrittenMemory |
| 32 | + { |
| 33 | + get |
| 34 | + { |
| 35 | + CheckIfDisposed(); |
| 36 | + |
| 37 | + return _rentedBuffer.AsMemory(0, _written); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public Span<T> WrittenSpan |
| 42 | + { |
| 43 | + get |
| 44 | + { |
| 45 | + CheckIfDisposed(); |
| 46 | + |
| 47 | + return _rentedBuffer.AsSpan(0, _written); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + public int BytesWritten |
| 52 | + { |
| 53 | + get |
| 54 | + { |
| 55 | + CheckIfDisposed(); |
| 56 | + |
| 57 | + return _written; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public long BytesCommitted |
| 62 | + { |
| 63 | + get |
| 64 | + { |
| 65 | + CheckIfDisposed(); |
| 66 | + |
| 67 | + return _committed; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public void Clear() |
| 72 | + { |
| 73 | + CheckIfDisposed(); |
| 74 | + |
| 75 | + ClearHelper(); |
| 76 | + } |
| 77 | + |
| 78 | + private void ClearHelper() |
| 79 | + { |
| 80 | + _rentedBuffer.AsSpan(0, _written).Clear(); |
| 81 | + _written = 0; |
| 82 | + } |
| 83 | + |
| 84 | + public void Advance(int count) |
| 85 | + { |
| 86 | + CheckIfDisposed(); |
| 87 | + |
| 88 | + if (count < 0) |
| 89 | + throw new ArgumentException(nameof(count)); |
| 90 | + |
| 91 | + if (_written > _rentedBuffer.Length - count) |
| 92 | + throw new InvalidOperationException("Cannot advance past the end of the buffer."); |
| 93 | + |
| 94 | + _written += count; |
| 95 | + } |
| 96 | + |
| 97 | + // Returns the rented buffer back to the pool |
| 98 | + public void Dispose() |
| 99 | + { |
| 100 | + if (_rentedBuffer == null) |
| 101 | + { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + ArrayPool<T>.Shared.Return(_rentedBuffer, clearArray: true); |
| 106 | + _rentedBuffer = null; |
| 107 | + _written = 0; |
| 108 | + } |
| 109 | + |
| 110 | + private void CheckIfDisposed() |
| 111 | + { |
| 112 | + if (_rentedBuffer == null) |
| 113 | + throw new ObjectDisposedException(nameof(ArrayBufferWriter<T>)); |
| 114 | + } |
| 115 | + |
| 116 | + public Memory<T> GetMemory(int sizeHint = 0) |
| 117 | + { |
| 118 | + CheckIfDisposed(); |
| 119 | + |
| 120 | + if (sizeHint < 0) |
| 121 | + throw new ArgumentException(nameof(sizeHint)); |
| 122 | + |
| 123 | + CheckAndResizeBuffer(sizeHint); |
| 124 | + return _rentedBuffer.AsMemory(_written); |
| 125 | + } |
| 126 | + |
| 127 | + public Span<T> GetSpan(int sizeHint = 0) |
| 128 | + { |
| 129 | + CheckIfDisposed(); |
| 130 | + |
| 131 | + if (sizeHint < 0) |
| 132 | + throw new ArgumentException(nameof(sizeHint)); |
| 133 | + |
| 134 | + CheckAndResizeBuffer(sizeHint); |
| 135 | + return _rentedBuffer.AsSpan(_written); |
| 136 | + } |
| 137 | + |
| 138 | + private void CheckAndResizeBuffer(int sizeHint) |
| 139 | + { |
| 140 | + Debug.Assert(sizeHint >= 0); |
| 141 | + |
| 142 | + if (sizeHint == 0) |
| 143 | + { |
| 144 | + sizeHint = MinimumBufferSize; |
| 145 | + } |
| 146 | + |
| 147 | + int availableSpace = _rentedBuffer.Length - _written; |
| 148 | + |
| 149 | + if (sizeHint > availableSpace) |
| 150 | + { |
| 151 | + int growBy = sizeHint > _rentedBuffer.Length ? sizeHint : _rentedBuffer.Length; |
| 152 | + |
| 153 | + int newSize = checked(_rentedBuffer.Length + growBy); |
| 154 | + |
| 155 | + T[] oldBuffer = _rentedBuffer; |
| 156 | + |
| 157 | + _rentedBuffer = ArrayPool<T>.Shared.Rent(newSize); |
| 158 | + |
| 159 | + Debug.Assert(oldBuffer.Length >= _written); |
| 160 | + Debug.Assert(_rentedBuffer.Length >= _written); |
| 161 | + |
| 162 | + oldBuffer.AsSpan(0, _written).CopyTo(_rentedBuffer); |
| 163 | + ArrayPool<T>.Shared.Return(oldBuffer, clearArray: true); |
| 164 | + } |
| 165 | + |
| 166 | + Debug.Assert(_rentedBuffer.Length - _written > 0); |
| 167 | + Debug.Assert(_rentedBuffer.Length - _written >= sizeHint); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
| 171 | +#endif |
0 commit comments