From 3c0511fa6af1e4f091101cc9eb034471b29783f8 Mon Sep 17 00:00:00 2001 From: Peter Petrov Date: Tue, 9 Sep 2025 21:38:15 +0300 Subject: [PATCH] [C#] Added ToSizedArrayPadded(int padLeft, int padRight) to ByteBuffers to avoid unnecessary copying. (#8658) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 Co-authored-by: Wouter van Oortmerssen --- net/FlatBuffers/ByteBuffer.cs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/net/FlatBuffers/ByteBuffer.cs b/net/FlatBuffers/ByteBuffer.cs index 0e959aec9..8e574d153 100644 --- a/net/FlatBuffers/ByteBuffer.cs +++ b/net/FlatBuffers/ByteBuffer.cs @@ -264,6 +264,22 @@ namespace Google.FlatBuffers } #endif + public T[] ToArrayPadded(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(Position, Length - Position, padLeft, padRight); + } + public byte[] ToSizedArray() { return ToArray(Position, Length - Position);