generateJsonBuffer.js 659 Bytes
Newer Older
Manggar Mahardhika's avatar
Manggar Mahardhika committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
import { defaultValue } from "../Source/Cesium.js";

export default function generateJsonBuffer(json, byteOffset, boundary) {
  var i;
  var jsonString = JSON.stringify(json);

  byteOffset = defaultValue(byteOffset, 0);
  boundary = defaultValue(boundary, 1);

  var byteLength = jsonString.length;
  var remainder = (byteOffset + byteLength) % boundary;
  var padding = remainder === 0 ? 0 : boundary - remainder;

  var buffer = new Uint8Array(byteLength + padding);

  for (i = 0; i < byteLength; ++i) {
    buffer[i] = jsonString.charCodeAt(i);
  }
  for (i = 0; i < padding; ++i) {
    buffer[byteLength + i] = 32; // Whitespace
  }

  return buffer;
}