Files
blitzkit/src/core/streams/byte.ts
T

67 lines
1.3 KiB
TypeScript
Raw Normal View History

2023-12-26 13:35:07 -06:00
export class ByteStream {
2023-12-25 12:37:37 -06:00
public index = 0;
2023-12-25 20:50:19 -06:00
constructor(public stream: Buffer) {}
2023-12-25 12:37:37 -06:00
skip(size: number) {
this.index += size;
}
read(size: number) {
2023-12-25 20:50:19 -06:00
return this.stream.subarray(this.index, this.index + size);
2023-12-25 12:37:37 -06:00
}
consume(size: number) {
const subarray = this.read(size);
this.skip(size);
return subarray;
}
readRemaining() {
return this.read(Number.POSITIVE_INFINITY);
}
readRemainingLength() {
2023-12-25 20:50:19 -06:00
return this.stream.length - this.index;
2023-12-25 12:37:37 -06:00
}
consumeRemaining() {
return this.consume(Number.POSITIVE_INFINITY);
}
2023-12-25 16:49:40 -06:00
byte(size: number) {
2023-12-25 12:37:37 -06:00
return this.consume(size);
}
2023-12-25 16:49:40 -06:00
ascii(size: number) {
2023-12-25 12:37:37 -06:00
return this.consume(size).toString('ascii');
}
int8() {
return this.consume(1).readInt8();
}
int16() {
return this.consume(2).readInt16LE();
}
int32() {
return this.consume(4).readInt32LE();
}
int64() {
return this.consume(8).readBigInt64LE();
}
uint8() {
return this.consume(1).readUInt8();
}
uint16() {
return this.consume(2).readUInt16LE();
}
uint32() {
return this.consume(4).readUInt32LE();
}
uint64() {
return this.consume(8).readBigUInt64LE();
}
float() {
return this.consume(4).readFloatLE();
}
boolean() {
return this.consume(1).readUInt8() === 1;
}
}