All files writer.ts

100% Statements 114/114
100% Branches 28/28
100% Functions 25/25
100% Lines 111/111

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229                  2x       42x       1x       8x       4x 1x 3x 1x     2x       6644x       1x       6645x 6645x 6645x 6645x       47474x 47474x   47474x 47470x     4x 2x     4x 4x 4x 4x       47474x 47474x 47472x         47456x 47456x 47456x       2x 2x 2x       2x 2x 2x       9422x 9422x 37808x 37808x 37808x 37808x 28386x   37808x         6582x 6582x 3284x 3284x   6582x 6582x 6582x 6582x 6058x     6582x   6582x 6058x         3x 3x 3x       2x 2x 2x       5x 5x 5x       1x 1x 1x       1x 1x 1x       4x 4x 33x         5x   5x 1x 1x     5x 5x   5x 48x 48x     5x   5x 48x 48x 48x         2x 2x 2x 2x 2x       2x 2x 13x 7x     2x       6x 6x   6x 115x 58x     115x   115x 13x 13x 13x       6x 4x        
import { IBufferWriter } from './types';
 
export class BufferWriter implements IBufferWriter {
	private _data: DataView;
	private _position: number;
	private _length: number;
	private _littleEndian: boolean;
 
	get littleEndian(): boolean {
		return this._littleEndian;
	}
 
	get length(): number {
		return this._length;
	}
 
	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.slice(0, this._length);
	}
 
	get bufferSize(): number {
		return this._data.buffer.byteLength;
	}
 
	constructor(bufferSize: number = 1024, littleEndian: boolean = false) {
		this._data = new DataView(new ArrayBuffer(bufferSize));
		this._position = 0;
		this._length = 0;
		this._littleEndian = littleEndian;
	}
 
	private allocate(bytes: number) {
		const targetSize = this._position + bytes;
		let size = this._data.buffer.byteLength;
 
		if (targetSize < size) {
			return;
		}
 
		while (size < targetSize) {
			size *= 2;
		}
 
		const buffer = new ArrayBuffer(size);
		const array = new Uint8Array(buffer);
		array.set(new Uint8Array(this._data.buffer));
		this._data = new DataView(buffer);
	}
 
	private movePosition(value: number) {
		this._position += value;
		if (this._length < this._position) {
			this._length = this._position;
		}
	}
 
	writeUint8(value: number) {
		this.allocate(1);
		this._data.setUint8(this._position, value);
		this.movePosition(1);
	}
 
	writeUint16(value: number) {
		this.allocate(2);
		this._data.setUint16(this._position, value, this._littleEndian);
		this.movePosition(2);
	}
 
	writeUint32(value: number) {
		this.allocate(4);
		this._data.setUint32(this._position, value, this._littleEndian);
		this.movePosition(4);
	}
 
	writeUintVar(value: number) {
		let next = false;
		do {
			let byte: number = value & 0x7f;
			value /= 128;
			next = value >= 1;
			if (next) {
				byte |= 0x80;
			}
			this.writeUint8(byte);
		} while (next);
	}
 
	writeIntVar(value: number) {
		let sign = 0;
		if (value < 0) {
			value = -value;
			sign = 1;
		}
		let byte: number = ((value & 0x3f) << 1) | sign;
		value /= 64;
		const next = value >= 1;
		if (next) {
			byte |= 0x80;
		}
 
		this.writeUint8(byte);
 
		if (next) {
			this.writeUintVar(value);
		}
	}
 
	writeInt8(value: number) {
		this.allocate(1);
		this._data.setInt8(this._position, value);
		this.movePosition(1);
	}
 
	writeInt16(value: number) {
		this.allocate(2);
		this._data.setInt16(this._position, value, this._littleEndian);
		this.movePosition(2);
	}
 
	writeInt32(value: number) {
		this.allocate(4);
		this._data.setInt32(this._position, value, this._littleEndian);
		this.movePosition(4);
	}
 
	writeFloat32(value: number) {
		this.allocate(4);
		this._data.setFloat32(this._position, value, this._littleEndian);
		this.movePosition(4);
	}
 
	writeFloat64(value: number) {
		this.allocate(8);
		this._data.setFloat64(this._position, value, this._littleEndian);
		this.movePosition(8);
	}
 
	writeString(value: string) {
		this.writeUintVar(value.length);
		for (let i = 0; i < value.length; i++) {
			this.writeUintVar(value.charCodeAt(i));
		}
	}
 
	writeBigInt(value: bigint) {
		let sign = 1;
 
		if (value < 0) {
			sign = -1;
			value = -value;
		}
 
		let temp = value;
		let count = 0;
 
		while (temp) {
			temp >>= 8n;
			count++;
		}
 
		this.writeIntVar(sign * count);
 
		while (value) {
			const byte = value & 255n;
			this.writeUint8(Number(byte));
			value >>= 8n;
		}
	}
 
	writeBuffer(value: ArrayBuffer) {
		this.writeUintVar(value.byteLength);
		this.allocate(value.byteLength);
		const array = new Uint8Array(this._data.buffer);
		array.set(new Uint8Array(value), this._position);
		this.movePosition(value.byteLength);
	}
 
	writeFlags(value: boolean[]) {
		let bitset = 0;
		for (let i = 0; i < value.length; i++) {
			if (value[i]) {
				bitset |= 2 ** i;
			}
		}
		this.writeUintVar(bitset);
	}
 
	writeBitset(value: boolean[]) {
		let bitset = 0;
		let position = 0;
 
		for (let i = 0; i < value.length; i++) {
			if (value[i]) {
				bitset |= 2 ** position;
			}
 
			position++;
 
			if (position === 8) {
				this.writeUint8(bitset);
				bitset = 0;
				position = 0;
			}
		}
 
		if (position) {
			this.writeUint8(bitset);
		}
	}
}