2022-08-11 06:22:27 +08:00
|
|
|
function generateJsonBuffer(json, byteOffset, boundary) {
|
2021-04-08 05:24:54 +08:00
|
|
|
let i;
|
|
|
|
|
const jsonString = JSON.stringify(json);
|
|
|
|
|
|
2025-03-04 03:03:53 +08:00
|
|
|
byteOffset = byteOffset ?? 0;
|
|
|
|
|
boundary = boundary ?? 1;
|
2021-04-08 05:24:54 +08:00
|
|
|
|
|
|
|
|
const byteLength = jsonString.length;
|
|
|
|
|
const remainder = (byteOffset + byteLength) % boundary;
|
|
|
|
|
const padding = remainder === 0 ? 0 : boundary - remainder;
|
|
|
|
|
|
|
|
|
|
const 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;
|
|
|
|
|
}
|
2022-08-11 06:22:27 +08:00
|
|
|
|
|
|
|
|
export default generateJsonBuffer;
|