UNPKG

@loaders.gl/json

Version:

Framework-independent loader for JSON and streaming JSON formats

39 lines (31 loc) 915 B
// loaders.gl // SPDX-License-Identifier: MIT import {ensureArrayBuffer} from '@loaders.gl/loader-utils'; /* global TextEncoder */ export class Utf8ArrayBufferEncoder { private readonly chunkSize: number; private strings: string[] = []; private totalLength = 0; private textEncoder: TextEncoder = new TextEncoder(); constructor(chunkSize: number) { this.chunkSize = chunkSize; } push(...strings: string[]): void { for (const string of strings) { this.strings.push(string); this.totalLength += string.length; } } isFull(): boolean { return this.totalLength >= this.chunkSize; } getArrayBufferBatch(): ArrayBuffer { return ensureArrayBuffer(this.textEncoder.encode(this.getStringBatch()).buffer); } getStringBatch(): string { const stringChunk = this.strings.join(''); this.strings = []; this.totalLength = 0; return stringChunk; } }