-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
108 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System.Buffers; | ||
|
||
namespace nyingi.Kafa.Writer | ||
{ | ||
internal sealed class KafaPooledWriter : IBufferWriter<byte>, IDisposable | ||
{ | ||
private const int MAXBUFFERLENGTH = 65556; | ||
private byte[] _buffer; | ||
|
||
private int _index = 0; | ||
|
||
public int WrittenCount => _index; | ||
public int Capacity => _buffer.Length; | ||
public int FreeCapacity => _buffer.Length - _index; | ||
|
||
public KafaPooledWriter(int length) | ||
{ | ||
_buffer = ArrayPool<byte>.Shared.Rent(Math.Max(length, MAXBUFFERLENGTH)); | ||
} | ||
public void Advance(int count) | ||
{ | ||
if(count < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(count)); | ||
} | ||
_index += count; | ||
} | ||
|
||
public Memory<byte> GetMemory(int sizeHint = 0) | ||
{ | ||
Resize(sizeHint); | ||
return _buffer.AsMemory(sizeHint); | ||
} | ||
|
||
public Span<byte> GetSpan(int sizeHint = 0) | ||
{ | ||
Resize(sizeHint); | ||
return _buffer.AsSpan(sizeHint); | ||
} | ||
|
||
|
||
public ReadOnlySpan<byte> WrittenAsSpan() => _buffer.AsSpan(0, _index); | ||
|
||
private void Resize(int sizeHint) | ||
{ | ||
if (sizeHint < 0) | ||
{ | ||
throw new ArgumentOutOfRangeException(nameof(sizeHint)); | ||
} | ||
|
||
if(sizeHint > FreeCapacity) | ||
{ | ||
int growBy = Math.Max(Capacity, sizeHint); | ||
int newCapacity = Capacity; | ||
checked | ||
{ | ||
newCapacity += growBy; | ||
} | ||
|
||
var newBuffer = ArrayPool<byte>.Shared.Rent(newCapacity); | ||
Array.Copy(_buffer, newBuffer, Capacity); | ||
_buffer = null!; | ||
_buffer = newBuffer; | ||
ArrayPool<byte>.Shared.Return(newBuffer); | ||
} | ||
} | ||
|
||
|
||
|
||
public void Dispose() | ||
{ | ||
if(_buffer == null) | ||
{ | ||
return; | ||
} | ||
ArrayPool<byte>.Shared.Return(_buffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System.Buffers; | ||
|
||
namespace nyingi.Kafa.Writer | ||
{ | ||
internal partial class KafaWriter : IDisposable | ||
{ | ||
private readonly IBufferWriter<byte> _bufferWriter; | ||
public int BytesWritten { get; private set; } | ||
private Stream? _stream = default; | ||
private Memory<byte> _memory = default; | ||
|
||
|
||
public KafaWriter(IBufferWriter<byte> bufferWriter) | ||
{ | ||
_bufferWriter = bufferWriter; | ||
} | ||
|
||
public KafaWriter(Stream stream) | ||
{ | ||
_stream = stream; | ||
} | ||
|
||
|
||
public void Dispose() | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |