MemoryWriter<T>
public abstract class MemoryWriter<T> : Disposable, IBufferWriter<T>, IConvertible<ReadOnlyMemory<T>>
Represents memory-backed output sink which T data can be written.
using System;
using System.Buffers;
using System.Runtime.CompilerServices;
namespace DotNext.Buffers
{
public abstract class MemoryWriter<[System.Runtime.CompilerServices.Nullable(2)] T> : Disposable, IBufferWriter<T>, IConvertible<ReadOnlyMemory<T>>
{
private protected const int DefaultInitialBufferSize = 256;
private protected int position;
[System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public abstract ReadOnlyMemory<T> WrittenMemory {
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
get;
}
public int WrittenCount {
get {
ThrowIfDisposed();
return position;
}
}
public abstract int Capacity { get; }
public int FreeCapacity {
get {
ThrowIfDisposed();
return Capacity - WrittenCount;
}
}
private protected MemoryWriter()
{
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
ReadOnlyMemory<T> IConvertible<ReadOnlyMemory<T>>.Convert()
{
return WrittenMemory;
}
public abstract void Clear();
public void Advance(int count)
{
if (count < 0)
throw new ArgumentOutOfRangeException("count");
if (position > Capacity - count)
throw new InvalidOperationException();
position += count;
}
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public abstract Memory<T> GetMemory(int sizeHint = 0);
[return: System.Runtime.CompilerServices.Nullable(new byte[] {
0,
1
})]
public abstract Span<T> GetSpan(int sizeHint = 0);
private protected abstract void Resize(int newSize);
private protected void CheckAndResizeBuffer(int sizeHint)
{
if (sizeHint < 0)
throw new ArgumentOutOfRangeException("sizeHint");
if (sizeHint == 0)
sizeHint = 1;
if (sizeHint > FreeCapacity) {
int capacity = Capacity;
int num = Math.Max(capacity, sizeHint);
if (capacity == 0)
num = Math.Max(num, 256);
int num2 = capacity + num;
if ((uint)num2 > 2147483647) {
num2 = capacity + sizeHint;
if ((uint)num2 > 2147483647)
throw new OutOfMemoryException();
}
Resize(num2);
}
}
}
}