UNPKG

byte-rw

Version:

Byte reader/writer for buffers and streams in typescript/javascript

47 lines (46 loc) 2.11 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { DataViewByteWriterAsync } from "./writer-async.js"; export class DataViewByteWriterAsyncChunked extends DataViewByteWriterAsync { constructor(littleEndian, defaultChunkSize = 4096) { super(new DataView(new ArrayBuffer(defaultChunkSize)), littleEndian); this.defaultChunkSize = defaultChunkSize; } complete() { const _super = Object.create(null, { complete: { get: () => super.complete } }); return __awaiter(this, void 0, void 0, function* () { const toSave = this._byteOffset; const saved = yield this.save(); if (saved != toSave) throw new Error("not all queued bytes could be saved"); yield _super.complete.call(this); }); } tryEnsureAvailable(bytes) { return __awaiter(this, void 0, void 0, function* () { if (this._isComplete) return 0; if (this._bytesRemaining < bytes) { const saved = yield this.save(); if (saved !== this._dataview.byteLength) { this._isComplete = true; return 0; } else { this._dataview = new DataView(new ArrayBuffer(Math.max(bytes, this.defaultChunkSize))); this._byteOffset = 0; } } return bytes; }); } }