[C#] Added ToSizedArrayPadded(int padLeft, int padRight) to ByteBuffers to avoid unnecessary copying. (#8658)

* Added ToSizedArrayPadded(int padLeft, int padRight) + ToArrayPadded(pos, len, padLeft, padRight) to the byteBuffers.
This is for API completion and to avoid unnecessary copy when framing my packets. I needed this to create a flat buffer with space in front of it for header / metadata.

* Fix indentation

---------

Co-authored-by: Björn Harrtell <bjornharrtell@users.noreply.github.com>
Co-authored-by: Wouter van Oortmerssen <aardappel@gmail.com>
This commit is contained in:
Peter Petrov
2025-09-09 21:38:15 +03:00
committed by GitHub
parent 82396fa0fe
commit 3c0511fa6a

View File

@@ -264,6 +264,22 @@ namespace Google.FlatBuffers
}
#endif
public T[] ToArrayPadded<T>(int pos, int len, int padLeft, int padRight)
where T : struct
{
AssertOffsetAndLength(pos, len);
int totalBytes = padLeft + len + padRight;
byte[] raw = _buffer.Buffer;
T[] arr = new T[totalBytes];
Buffer.BlockCopy(raw, pos, arr, padLeft, len);
return arr;
}
public byte[] ToSizedArrayPadded(int padLeft, int padRight)
{
return ToArrayPadded<byte>(Position, Length - Position, padLeft, padRight);
}
public byte[] ToSizedArray()
{
return ToArray<byte>(Position, Length - Position);