obj2gltf/lib/getBufferPadded.js

23 lines
618 B
JavaScript
Raw Normal View History

2017-07-19 17:56:24 -04:00
'use strict';
module.exports = getBufferPadded;
/**
* Pad the buffer to the next 4-byte boundary to ensure proper alignment for the section that follows.
*
* @param {Buffer} buffer The buffer.
* @returns {Buffer} The padded buffer.
*
* @private
*/
function getBufferPadded(buffer) {
2019-02-05 20:59:09 -05:00
const boundary = 4;
const byteLength = buffer.length;
const remainder = byteLength % boundary;
2017-07-24 09:36:45 -04:00
if (remainder === 0) {
return buffer;
}
2019-02-05 20:59:09 -05:00
const padding = (remainder === 0) ? 0 : boundary - remainder;
const emptyBuffer = Buffer.alloc(padding);
2017-07-19 17:56:24 -04:00
return Buffer.concat([buffer, emptyBuffer]);
}