| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Buffers; |
| | 6 | | using System.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Azure.Core |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Represents a heap-based, array-backed output sink into which <typeparam name="T"/> data can be written. |
| | 12 | | /// </summary> |
| | 13 | | internal sealed class ArrayBufferWriter<T> : IBufferWriter<T> |
| | 14 | | { |
| | 15 | | private T[] _buffer; |
| | 16 | | private const int DefaultInitialBufferSize = 256; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Creates an instance of an <see cref="ArrayBufferWriter{T}"/>, in which data can be written to, |
| | 20 | | /// with the default initial capacity. |
| | 21 | | /// </summary> |
| 144 | 22 | | public ArrayBufferWriter() |
| | 23 | | { |
| 144 | 24 | | _buffer = Array.Empty<T>(); |
| 144 | 25 | | WrittenCount = 0; |
| 144 | 26 | | } |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Creates an instance of an <see cref="ArrayBufferWriter{T}"/>, in which data can be written to, |
| | 30 | | /// with an initial capacity specified. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="initialCapacity">The minimum capacity with which to initialize the underlying buffer.</param> |
| | 33 | | /// <exception cref="ArgumentException"> |
| | 34 | | /// Thrown when <paramref name="initialCapacity"/> is not positive (i.e. less than or equal to 0). |
| | 35 | | /// </exception> |
| 0 | 36 | | public ArrayBufferWriter(int initialCapacity) |
| | 37 | | { |
| 0 | 38 | | if (initialCapacity <= 0) |
| | 39 | | #pragma warning disable CA2208 // Instantiate argument exceptions correctly |
| 0 | 40 | | throw new ArgumentException(nameof(initialCapacity)); |
| | 41 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 42 | |
|
| 0 | 43 | | _buffer = new T[initialCapacity]; |
| 0 | 44 | | WrittenCount = 0; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Returns the data written to the underlying buffer so far, as a <see cref="ReadOnlyMemory{T}"/>. |
| | 49 | | /// </summary> |
| 144 | 50 | | public ReadOnlyMemory<T> WrittenMemory => _buffer.AsMemory(0, WrittenCount); |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Returns the data written to the underlying buffer so far, as a <see cref="ReadOnlySpan{T}"/>. |
| | 54 | | /// </summary> |
| 0 | 55 | | public ReadOnlySpan<T> WrittenSpan => _buffer.AsSpan(0, WrittenCount); |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Returns the amount of data written to the underlying buffer so far. |
| | 59 | | /// </summary> |
| 1508 | 60 | | public int WrittenCount { get; private set; } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// Returns the total amount of space within the underlying buffer. |
| | 64 | | /// </summary> |
| 0 | 65 | | public int Capacity => _buffer.Length; |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Returns the amount of space available that can still be written into without forcing the underlying buffer t |
| | 69 | | /// </summary> |
| 244 | 70 | | public int FreeCapacity => _buffer.Length - WrittenCount; |
| | 71 | |
|
| | 72 | | /// <summary> |
| | 73 | | /// Clears the data written to the underlying buffer. |
| | 74 | | /// </summary> |
| | 75 | | /// <remarks> |
| | 76 | | /// You must clear the <see cref="ArrayBufferWriter{T}"/> before trying to re-use it. |
| | 77 | | /// </remarks> |
| | 78 | | public void Clear() |
| | 79 | | { |
| | 80 | | Debug.Assert(_buffer.Length >= WrittenCount); |
| 0 | 81 | | _buffer.AsSpan(0, WrittenCount).Clear(); |
| 0 | 82 | | WrittenCount = 0; |
| 0 | 83 | | } |
| | 84 | |
|
| | 85 | | /// <summary> |
| | 86 | | /// Notifies <see cref="IBufferWriter{T}"/> that <paramref name="count"/> amount of data was written to the outp |
| | 87 | | /// </summary> |
| | 88 | | /// <exception cref="ArgumentException"> |
| | 89 | | /// Thrown when <paramref name="count"/> is negative. |
| | 90 | | /// </exception> |
| | 91 | | /// <exception cref="InvalidOperationException"> |
| | 92 | | /// Thrown when attempting to advance past the end of the underlying buffer. |
| | 93 | | /// </exception> |
| | 94 | | /// <remarks> |
| | 95 | | /// You must request a new buffer after calling Advance to continue writing more data and cannot write to a prev |
| | 96 | | /// </remarks> |
| | 97 | | public void Advance(int count) |
| | 98 | | { |
| 244 | 99 | | if (count < 0) |
| | 100 | | #pragma warning disable CA2208 // Instantiate argument exceptions correctly |
| 0 | 101 | | throw new ArgumentException(nameof(count)); |
| | 102 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 103 | |
|
| 244 | 104 | | if (WrittenCount > _buffer.Length - count) |
| 0 | 105 | | ThrowInvalidOperationException_AdvancedTooFar(_buffer.Length); |
| | 106 | |
|
| 244 | 107 | | WrittenCount += count; |
| 244 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <summary> |
| | 111 | | /// Returns a <see cref="Memory{T}"/> to write to that is at least the requested length (specified by <paramref |
| | 112 | | /// If no <paramref name="sizeHint"/> is provided (or it's equal to <code>0</code>), some non-empty buffer is re |
| | 113 | | /// </summary> |
| | 114 | | /// <exception cref="ArgumentException"> |
| | 115 | | /// Thrown when <paramref name="sizeHint"/> is negative. |
| | 116 | | /// </exception> |
| | 117 | | /// <remarks> |
| | 118 | | /// This will never return an empty <see cref="Memory{T}"/>. |
| | 119 | | /// </remarks> |
| | 120 | | /// <remarks> |
| | 121 | | /// There is no guarantee that successive calls will return the same buffer or the same-sized buffer. |
| | 122 | | /// </remarks> |
| | 123 | | /// <remarks> |
| | 124 | | /// You must request a new buffer after calling Advance to continue writing more data and cannot write to a prev |
| | 125 | | /// </remarks> |
| | 126 | | public Memory<T> GetMemory(int sizeHint = 0) |
| | 127 | | { |
| 244 | 128 | | CheckAndResizeBuffer(sizeHint); |
| | 129 | | Debug.Assert(_buffer.Length > WrittenCount); |
| 244 | 130 | | return _buffer.AsMemory(WrittenCount); |
| | 131 | | } |
| | 132 | |
|
| | 133 | | /// <summary> |
| | 134 | | /// Returns a <see cref="Span{T}"/> to write to that is at least the requested length (specified by <paramref na |
| | 135 | | /// If no <paramref name="sizeHint"/> is provided (or it's equal to <code>0</code>), some non-empty buffer is re |
| | 136 | | /// </summary> |
| | 137 | | /// <exception cref="ArgumentException"> |
| | 138 | | /// Thrown when <paramref name="sizeHint"/> is negative. |
| | 139 | | /// </exception> |
| | 140 | | /// <remarks> |
| | 141 | | /// This will never return an empty <see cref="Span{T}"/>. |
| | 142 | | /// </remarks> |
| | 143 | | /// <remarks> |
| | 144 | | /// There is no guarantee that successive calls will return the same buffer or the same-sized buffer. |
| | 145 | | /// </remarks> |
| | 146 | | /// <remarks> |
| | 147 | | /// You must request a new buffer after calling Advance to continue writing more data and cannot write to a prev |
| | 148 | | /// </remarks> |
| | 149 | | public Span<T> GetSpan(int sizeHint = 0) |
| | 150 | | { |
| 0 | 151 | | CheckAndResizeBuffer(sizeHint); |
| | 152 | | Debug.Assert(_buffer.Length > WrittenCount); |
| 0 | 153 | | return _buffer.AsSpan(WrittenCount); |
| | 154 | | } |
| | 155 | |
|
| | 156 | | private void CheckAndResizeBuffer(int sizeHint) |
| | 157 | | { |
| 244 | 158 | | if (sizeHint < 0) |
| | 159 | | #pragma warning disable CA2208 // Instantiate argument exceptions correctly |
| 0 | 160 | | throw new ArgumentException(nameof(sizeHint)); |
| | 161 | | #pragma warning restore CA2208 // Instantiate argument exceptions correctly |
| | 162 | |
|
| 244 | 163 | | if (sizeHint == 0) |
| | 164 | | { |
| 0 | 165 | | sizeHint = 1; |
| | 166 | | } |
| | 167 | |
|
| 244 | 168 | | if (sizeHint > FreeCapacity) |
| | 169 | | { |
| 244 | 170 | | int growBy = Math.Max(sizeHint, _buffer.Length); |
| | 171 | |
|
| 244 | 172 | | if (_buffer.Length == 0) |
| | 173 | | { |
| 144 | 174 | | growBy = Math.Max(growBy, DefaultInitialBufferSize); |
| | 175 | | } |
| | 176 | |
|
| 244 | 177 | | int newSize = checked(_buffer.Length + growBy); |
| | 178 | |
|
| 244 | 179 | | Array.Resize(ref _buffer, newSize); |
| | 180 | | } |
| | 181 | |
|
| | 182 | | Debug.Assert(FreeCapacity > 0 && FreeCapacity >= sizeHint); |
| 244 | 183 | | } |
| | 184 | |
|
| | 185 | | private static void ThrowInvalidOperationException_AdvancedTooFar(int capacity) |
| | 186 | | { |
| 0 | 187 | | throw new InvalidOperationException($"Advanced past capacity of {capacity}"); |
| | 188 | | } |
| | 189 | | } |
| | 190 | | } |