UNPKG

@loaders.gl/json

Version:

Framework-independent loader for JSON and streaming JSON formats

31 lines (30 loc) 779 B
// loaders.gl // SPDX-License-Identifier: MIT /* global TextEncoder */ export class Utf8ArrayBufferEncoder { chunkSize; strings = []; totalLength = 0; textEncoder = new TextEncoder(); constructor(chunkSize) { this.chunkSize = chunkSize; } push(...strings) { for (const string of strings) { this.strings.push(string); this.totalLength += string.length; } } isFull() { return this.totalLength >= this.chunkSize; } getArrayBufferBatch() { return this.textEncoder.encode(this.getStringBatch()).buffer; } getStringBatch() { const stringChunk = this.strings.join(''); this.strings = []; this.totalLength = 0; return stringChunk; } }