All files reader.ts

100% Statements 93/93
100% Branches 19/19
100% Functions 24/24
100% Lines 92/92

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206                2x       31x       2x       8x       4x 1x 3x 1x     2x       2x       1x       6643x 6643x 6643x       44472x               44456x 44456x 44456x       1x 1x 1x       2x 2x 2x       9422x 9422x 9422x   9422x 37808x 37808x 37808x     9422x       6582x 6582x 6582x   6582x 6058x     6582x       2x 2x 2x       3x 3x 3x       4x 4x 4x       1x 1x 1x       1x 1x 1x       4x 4x 1x     3x 3x 33x   3x       5x 5x   5x 1x 1x     5x   5x   5x 48x 48x 48x     5x       2x 2x       2x 2x       2x 2x   2x 13x     2x       6x   6x 5x 5x   5x 115x 115x 115x 12x 12x         6x      
import { IBufferReader } from './types';
 
export class BufferReader implements IBufferReader {
	private _data: DataView;
	private _position: number;
	private _littleEndian: boolean;
 
	get littleEndian(): boolean {
		return this._littleEndian;
	}
 
	get length(): number {
		return this._data.buffer.byteLength;
	}
 
	get bytesAvailable(): number {
		return this.length - this._position;
	}
 
	get position(): number {
		return this._position;
	}
 
	set position(value: number) {
		if (value < 0) {
			throw 'out of range';
		} else if (value > this.length) {
			throw 'out of range';
		}
 
		this._position = value;
	}
 
	get buffer(): ArrayBuffer {
		return this._data.buffer;
	}
 
	get view(): DataView {
		return this._data;
	}
 
	constructor(buffer: ArrayBuffer, littleEndian: boolean = false) {
		this._data = new DataView(buffer);
		this._position = 0;
		this._littleEndian = littleEndian;
	}
 
	private movePosition(value: number) {
		this._position += value;
	}
 
	// getUint8(): number {
	// 	return this._data.getUint8(this._position);
	// }
 
	readUint8(): number {
		const value = this._data.getUint8(this._position);
		this.movePosition(1);
		return value;
	}
 
	readUint16(): number {
		const value = this._data.getUint16(this._position, this._littleEndian);
		this.movePosition(2);
		return value;
	}
 
	readUint32(): number {
		const value = this._data.getUint32(this._position, this._littleEndian);
		this.movePosition(4);
		return value;
	}
 
	readUintVar(): number {
		let value = 0;
		let offset = 1;
		let byte = 0;
 
		do {
			byte = this.readUint8();
			value += (byte & 0x7f) * offset;
			offset *= 128;
		} while (byte & 0x80);
 
		return value;
	}
 
	readIntVar(): number {
		const byte = this.readUint8();
		const sign = byte & 1 ? -1 : 1;
		let value = (byte >>> 1) & 0x3f;
 
		if (byte & 0x80) {
			value += this.readUintVar() * 64;
		}
 
		return value * sign;
	}
 
	readInt8(): number {
		const value = this._data.getInt8(this._position);
		this.movePosition(1);
		return value;
	}
 
	readInt16(): number {
		const value = this._data.getInt16(this._position, this._littleEndian);
		this.movePosition(2);
		return value;
	}
 
	readInt32(): number {
		const value = this._data.getInt32(this._position, this._littleEndian);
		this.movePosition(4);
		return value;
	}
 
	readFloat32(): number {
		const value = this._data.getFloat32(this._position, this._littleEndian);
		this.movePosition(4);
		return value;
	}
 
	readFloat64(): number {
		const value = this._data.getFloat64(this._position, this._littleEndian);
		this.movePosition(8);
		return value;
	}
 
	readString(): string {
		let count = this.readUintVar();
		if (!count) {
			return '';
		}
 
		let value = '';
		while (count--) {
			value += String.fromCharCode(this.readUintVar());
		}
		return value;
	}
 
	readBigInt(): bigint {
		let count = this.readIntVar();
		let sign = 1n;
 
		if (count < 0) {
			count = -count;
			sign = -1n;
		}
 
		let value = 0n;
 
		let i = 0n;
 
		while (count--) {
			const byte = BigInt(this.readUint8());
			value |= byte << i;
			i += 8n;
		}
 
		return value * sign;
	}
 
	readBuffer(): ArrayBuffer {
		const count = this.readUintVar();
		const value = this._data.buffer.slice(
			this._position,
			this._position + count,
		);
		this.movePosition(value.byteLength);
		return value;
	}
 
	readFlags(count: number): boolean[] {
		const value = this.readUintVar();
		const flags: boolean[] = [];
 
		for (let i = 0; i < count; i++) {
			flags.push(!!(value & (2 ** i)));
		}
 
		return flags;
	}
 
	readBitset(count: number): boolean[] {
		const value: boolean[] = [];
 
		if (count) {
			let bitset = this.readUint8();
			let position = 0;
 
			while (count--) {
				value.push(!!(bitset & (2 ** position)));
				position++;
				if (position === 8 && count) {
					bitset = this.readUint8();
					position = 0;
				}
			}
		}
 
		return value;
	}
}