diff --git a/net/FlatBuffers/FlatBufferBuilder.cs b/net/FlatBuffers/FlatBufferBuilder.cs index 90627fdfd..77fdcfc9d 100644 --- a/net/FlatBuffers/FlatBufferBuilder.cs +++ b/net/FlatBuffers/FlatBufferBuilder.cs @@ -580,6 +580,25 @@ namespace FlatBuffers } /// @endcond + /// + /// Finalize a buffer, pointing to the given `root_table`. + /// + /// + /// An offset to be added to the buffer. + /// + /// + /// Whether to prefix the size to the buffer. + /// + protected void Finish(int rootTable, bool sizePrefix) + { + Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0)); + AddOffset(rootTable); + if (sizePrefix) { + AddInt(_bb.Length - _space); + } + _bb.Position = _space; + } + /// /// Finalize a buffer, pointing to the given `root_table`. /// @@ -588,9 +607,18 @@ namespace FlatBuffers /// public void Finish(int rootTable) { - Prep(_minAlign, sizeof(int)); - AddOffset(rootTable); - _bb.Position = _space; + Finish(rootTable, false); + } + + /// + /// Finalize a buffer, pointing to the given `root_table`, with the size prefixed. + /// + /// + /// An offset to be added to the buffer. + /// + public void FinishSizePrefixed(int rootTable) + { + Finish(rootTable, true); } /// @@ -621,35 +649,66 @@ namespace FlatBuffers return newArray; } - /// - /// Finalize a buffer, pointing to the given `rootTable`. - /// - /// - /// An offset to be added to the buffer. - /// - /// - /// A FlatBuffer file identifier to be added to the buffer before - /// `root_table`. - /// - public void Finish(int rootTable, string fileIdentifier) - { - Prep(_minAlign, sizeof(int) + - FlatBufferConstants.FileIdentifierLength); - if (fileIdentifier.Length != - FlatBufferConstants.FileIdentifierLength) - throw new ArgumentException( - "FlatBuffers: file identifier must be length " + - FlatBufferConstants.FileIdentifierLength, - "fileIdentifier"); - for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0; - i--) - { - AddByte((byte)fileIdentifier[i]); - } - Finish(rootTable); + /// + /// Finalize a buffer, pointing to the given `rootTable`. + /// + /// + /// An offset to be added to the buffer. + /// + /// + /// A FlatBuffer file identifier to be added to the buffer before + /// `root_table`. + /// + /// + /// Whether to prefix the size to the buffer. + /// + protected void Finish(int rootTable, string fileIdentifier, bool sizePrefix) + { + Prep(_minAlign, sizeof(int) + (sizePrefix ? sizeof(int) : 0) + + FlatBufferConstants.FileIdentifierLength); + if (fileIdentifier.Length != + FlatBufferConstants.FileIdentifierLength) + throw new ArgumentException( + "FlatBuffers: file identifier must be length " + + FlatBufferConstants.FileIdentifierLength, + "fileIdentifier"); + for (int i = FlatBufferConstants.FileIdentifierLength - 1; i >= 0; + i--) + { + AddByte((byte)fileIdentifier[i]); + } + Finish(rootTable, sizePrefix); } + /// + /// Finalize a buffer, pointing to the given `rootTable`. + /// + /// + /// An offset to be added to the buffer. + /// + /// + /// A FlatBuffer file identifier to be added to the buffer before + /// `root_table`. + /// + public void Finish(int rootTable, string fileIdentifier) + { + Finish(rootTable, fileIdentifier, false); + } + /// + /// Finalize a buffer, pointing to the given `rootTable`, with the size prefixed. + /// + /// + /// An offset to be added to the buffer. + /// + /// + /// A FlatBuffer file identifier to be added to the buffer before + /// `root_table`. + /// + public void FinishSizePrefixed(int rootTable, string fileIdentifier) + { + Finish(rootTable, fileIdentifier, true); + } } }