UNPKG

byte-rw

Version:

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

168 lines (167 loc) 6.38 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 { copy, textEncoder } from "../utils/index.js"; export class DataViewByteWriterAsync { get _bytesRemaining() { return this._dataview.byteLength - this._byteOffset; } get dataview() { return this._dataview; } set dataview(dataview) { this._dataview = dataview; } isComplete() { return __awaiter(this, void 0, void 0, function* () { return this._isComplete || (this._isComplete = yield this.updateIsComplete()); }); } constructor(dataview, littleEndian = true) { this.littleEndian = littleEndian; this._isComplete = false; this._byteOffset = 0; this._dataview = dataview; } updateIsComplete() { return __awaiter(this, void 0, void 0, function* () { return (yield this.tryEnsureAvailable(1)) > 0; }); } complete() { return __awaiter(this, void 0, void 0, function* () { this._isComplete = true; }); } /** * Attempts to ensure that a specified number of bytes are available in the * current dataview. * * @param bytes the number of bytes to request available in the current * dataview * @returns the number of bytes at least made available in the current * dataview, up to the requested number of bytes */ tryEnsureAvailable(bytes) { return __awaiter(this, void 0, void 0, function* () { if (this._isComplete) return 0; if (this._bytesRemaining < bytes) return this._bytesRemaining; else return bytes; }); } ensureAvailable(bytes) { return __awaiter(this, void 0, void 0, function* () { const read = yield this.tryEnsureAvailable(bytes); if (read !== bytes) throw new Error(`Not all bytes could be available (${bytes} bytes requested, ${read} bytes available)`); }); } writeFloat32(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 4; yield this.ensureAvailable(bytes); this._dataview.setFloat32(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } writeFloat64(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 8; yield this.ensureAvailable(bytes); this._dataview.setFloat64(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } writeInt8(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 1; yield this.ensureAvailable(bytes); this._dataview.setInt8(this._byteOffset, value); this._byteOffset += bytes; }); } writeInt16(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 2; yield this.ensureAvailable(bytes); this._dataview.setInt16(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } writeInt32(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 4; yield this.ensureAvailable(bytes); this._dataview.setInt32(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } writeUint8(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 1; yield this.ensureAvailable(bytes); this._dataview.setUint8(this._byteOffset, value); this._byteOffset += bytes; }); } writeUint16(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 2; yield this.ensureAvailable(bytes); this._dataview.setUint16(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } writeUint32(value) { return __awaiter(this, void 0, void 0, function* () { const bytes = 4; yield this.ensureAvailable(bytes); this._dataview.setUint32(this._byteOffset, value, this.littleEndian); this._byteOffset += bytes; }); } tryWriteBytes(view) { return __awaiter(this, void 0, void 0, function* () { const bytes = view.byteLength; const write = yield this.tryEnsureAvailable(bytes); const dst = this._dataview.buffer; const src = view.buffer; const dstOffset = this._dataview.byteOffset + this._byteOffset; const srcOffset = view.byteOffset; copy({ buffer: src, byteOffset: srcOffset, byteLength: write, }, { buffer: dst, byteOffset: dstOffset, byteLength: write, }); this._byteOffset += write; return write; }); } writeBytes(view) { return __awaiter(this, void 0, void 0, function* () { const written = yield this.tryWriteBytes(view); if (written !== view.byteLength) throw new Error(`Not all bytes could be written (${view.byteLength} bytes requested, ${written} bytes written)`); }); } writeString(value) { return __awaiter(this, void 0, void 0, function* () { const encoded = textEncoder.encode(value); yield this.writeUint32(encoded.byteLength); yield this.writeBytes(encoded); }); } }