Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/MongoDB.Bson/IO/EncodingHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,33 @@ internal static class EncodingHelper
{
public readonly struct DisposableSegment : IDisposable
{
private IDisposable DisposableData { get; }
private bool DisposeRentedBuffer { get; }
private ThreadStaticBuffer.RentedBuffer RentedBuffer { get; }
public ArraySegment<byte> Segment { get; }

public DisposableSegment(IDisposable disposableData, ArraySegment<byte> segment)
public DisposableSegment(ThreadStaticBuffer.RentedBuffer rentedBuffer, ArraySegment<byte> segment)
{
DisposableData = disposableData;
DisposeRentedBuffer = true;
RentedBuffer = rentedBuffer;
Segment = segment;
}

public DisposableSegment(ArraySegment<byte> segment)
{
DisposeRentedBuffer = false;
Segment = segment;
}

public void Dispose()
{
DisposableData?.Dispose();
if (DisposeRentedBuffer)
{
RentedBuffer.Dispose();
}
}
}

private static readonly ArraySegment<byte> __emptySegment = new ArraySegment<byte>(new byte[0]);
private static readonly ArraySegment<byte> __emptySegment = new(new byte[0]);

public static DisposableSegment GetBytesUsingThreadStaticBuffer(this Encoding encoding, string value)
{
Expand All @@ -57,7 +68,7 @@ public static DisposableSegment GetBytesUsingThreadStaticBuffer(this Encoding en
var length = value.Length;
if (length == 0)
{
return new DisposableSegment(null, __emptySegment);
return new DisposableSegment(__emptySegment);
}

var maxSize = encoding.GetMaxByteCount(length);
Expand Down
Loading