flatbuffers
flatbuffers copied to clipboard
FR [C#]: Allow parsing IList<byte> objects as Flatbuffer tables
Right now to parse data as a Flatbuffer table (in C#) requires a ByteBuffer which must be built from a byte array. This means if I have something that looks like an array (such as a ArraySegment) I have to make a copy before I can interpret it as a Flatbuffer table.
Instead, it would be nice if we could take any object which allows indexing (such as IList) and interpret it as a flatbuffer.
To implement this we can't simply allow ByteBuffer to be built from any IList type because ByteBuffer supports a bunch of mutating operations used for constructing Flatbuffer objects. Instead I think what would be needed is a new ReadOnlyByteBuffer which supports just the reading operations.
Such indirections sometimes come with performance tradeoffs, and it is pretty important that basic reading/writing of bytes is not impacted. @dbaileychess
I'm open to PRs to see how it would look. I'm not looking to implement this myself at the moment.
I came across this exact scenario where I was using pooled byte arrays that were larger than the actual content and solved it this way.
/// <summary>
/// A ByteBufferAllocator that does not require the byte[] to be exactly sized
/// </summary>
public class ArraySegmentByteBufferAllocator : FlatBuffers.ByteBufferAllocator
{
public ArraySegmentByteBufferAllocator(byte[] buffer, int length)
{
this.Buffer = buffer;
this.Length = length;
}
public override void GrowFront(int newSize)
{
throw new NotImplementedException();
}
}
Then you can make a ByteBuffer using that allocator:
byte[] oversizedByteArray = null;
int actualContentLength = 0;
var fbByteBuffer = new FlatBuffers.ByteBuffer(new ArraySegmentByteBufferAllocator(oversizedByteArray, actualContentLength), 0);
This issue is stale because it has been open 6 months with no activity. Please comment or label not-stale
, or this will be closed in 14 days.
This issue was automatically closed due to no activity for 6 months plus the 14 day notice period.