From 34cb163e389e928db08ed2bd0e16ee0ac53ab1ce Mon Sep 17 00:00:00 2001 From: Mark Henderson Date: Mon, 23 Apr 2018 12:54:20 -0700 Subject: [PATCH] Adding JS function to get the File Identifier (#4715) * Adding JS function to get the File Identifier * Update flatbuffers.js --- js/flatbuffers.js | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/js/flatbuffers.js b/js/flatbuffers.js index 4c2bf52d2..580020a49 100644 --- a/js/flatbuffers.js +++ b/js/flatbuffers.js @@ -1040,6 +1040,26 @@ flatbuffers.ByteBuffer.prototype.writeFloat64 = function(offset, value) { this.writeInt32(offset + 4, flatbuffers.int32[flatbuffers.isLittleEndian ? 1 : 0]); }; +/** + * Return the file identifier. Behavior is undefined for FlatBuffers whose + * schema does not include a file_identifier (likely points at padding or the + * start of a the root vtable). + * @returns {string} + */ +flatbuffers.ByteBuffer.prototype.getBufferIdentifier = function() { + if (this.bytes_.length < this.position_ + flatbuffers.SIZEOF_INT + + flatbuffers.FILE_IDENTIFIER_LENGTH) { + throw new Error( + 'FlatBuffers: ByteBuffer is too short to contain an identifier.'); + } + var result = ""; + for (var i = 0; i < flatbuffers.FILE_IDENTIFIER_LENGTH; i++) { + result += String.fromCharCode( + this.readInt8(this.position_ + flatbuffers.SIZEOF_INT + i)); + } + return result; +}; + /** * Look up a field in the vtable, return an offset into the object, or 0 if the * field is not present.