Skip to content

Commit 3c0511f

Browse files
petersvpbjornharrtellaardappel
authored
[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 <[email protected]> Co-authored-by: Wouter van Oortmerssen <[email protected]>
1 parent 82396fa commit 3c0511f

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

net/FlatBuffers/ByteBuffer.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,22 @@ public T[] ToArray<T>(int pos, int len)
264264
}
265265
#endif
266266

267+
public T[] ToArrayPadded<T>(int pos, int len, int padLeft, int padRight)
268+
where T : struct
269+
{
270+
AssertOffsetAndLength(pos, len);
271+
int totalBytes = padLeft + len + padRight;
272+
byte[] raw = _buffer.Buffer;
273+
T[] arr = new T[totalBytes];
274+
Buffer.BlockCopy(raw, pos, arr, padLeft, len);
275+
return arr;
276+
}
277+
278+
public byte[] ToSizedArrayPadded(int padLeft, int padRight)
279+
{
280+
return ToArrayPadded<byte>(Position, Length - Position, padLeft, padRight);
281+
}
282+
267283
public byte[] ToSizedArray()
268284
{
269285
return ToArray<byte>(Position, Length - Position);

0 commit comments

Comments
 (0)