using System.Buffers; namespace NBConfigBuilder; public enum MemoryStreamBufferSource { None = 0, Pack = 1, UnPack = 2, } public sealed class MemoryStreamBuffer : MemoryStream, IBufferWriter { public MemoryStreamBufferSource MemoryStreamBufferSource; public MemoryStreamBuffer() { } public MemoryStreamBuffer(MemoryStreamBufferSource memoryStreamBufferSource, int capacity) : base(capacity) { MemoryStreamBufferSource = memoryStreamBufferSource; } public MemoryStreamBuffer(byte[] buffer) : base(buffer) { } public void Advance(int count) { if (count < 0) { throw new ArgumentOutOfRangeException(nameof(count), count, "The value of 'count' cannot be negative."); } var newLength = Position + count; if (newLength != Length) { SetLength(newLength); } Position = newLength; } public Memory GetMemory(int sizeHint = 0) { if (sizeHint < 0) { throw new ArgumentOutOfRangeException(nameof(sizeHint), sizeHint, "The value of 'count' cannot be negative."); } if (Length - Position <= sizeHint) { SetLength(Position + sizeHint); } return new Memory(GetBuffer(), (int)Position, (int)(Length - Position)); } public Span GetSpan(int sizeHint = 0) { if (sizeHint < 0) { throw new ArgumentOutOfRangeException(nameof(sizeHint), sizeHint, "The value of 'count' cannot be negative."); } if (Length - Position <= sizeHint) { SetLength(Position + sizeHint); } return new Span(GetBuffer(), (int)Position, (int)(Length - Position)); } }