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

73 lines
1.6 KiB
TypeScript
Raw Normal View History

2023-12-26 20:02:01 -06:00
export class BufferStream {
2023-12-25 12:37:37 -06:00
public index = 0;
2024-01-29 15:53:47 -06:00
constructor(public buffer: Buffer) {}
2023-12-25 12:37:37 -06:00
skip(size: number) {
this.index += size;
}
read(size: number) {
2024-01-29 15:53:47 -06:00
return this.buffer.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;
}
2024-01-29 15:53:47 -06:00
increment(size: number) {
return (this.index += size) - size;
}
2023-12-25 12:37:37 -06:00
readRemaining() {
return this.read(Number.POSITIVE_INFINITY);
}
readRemainingLength() {
2024-01-29 15:53:47 -06:00
return this.buffer.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) {
2024-01-29 15:53:47 -06:00
return this.buffer.toString('ascii', this.increment(size), this.index);
2023-12-25 12:37:37 -06:00
}
int8() {
2024-01-29 15:53:47 -06:00
return this.buffer.readInt8(this.increment(1));
2023-12-25 12:37:37 -06:00
}
int16() {
2024-01-29 15:53:47 -06:00
return this.buffer.readInt16LE(this.increment(2));
2023-12-25 12:37:37 -06:00
}
int32() {
2024-01-29 15:53:47 -06:00
return this.buffer.readInt32LE(this.increment(4));
2023-12-25 12:37:37 -06:00
}
int64() {
2024-01-29 15:53:47 -06:00
return this.buffer.readBigInt64LE(this.increment(8));
2023-12-25 12:37:37 -06:00
}
uint8() {
2024-01-29 15:53:47 -06:00
return this.buffer.readUInt8(this.increment(1));
2023-12-25 12:37:37 -06:00
}
uint16() {
2024-01-29 15:53:47 -06:00
return this.buffer.readUInt16LE(this.increment(2));
2023-12-25 12:37:37 -06:00
}
uint32() {
2024-01-29 15:53:47 -06:00
return this.buffer.readUInt32LE(this.increment(4));
2023-12-25 12:37:37 -06:00
}
uint64() {
2024-01-29 15:53:47 -06:00
return this.buffer.readBigUInt64LE(this.increment(8));
2023-12-25 12:37:37 -06:00
}
2024-02-04 11:06:26 -06:00
float32() {
2024-01-29 15:53:47 -06:00
return this.buffer.readFloatLE(this.increment(4));
2023-12-25 12:37:37 -06:00
}
2024-02-04 11:06:26 -06:00
float64() {
return this.buffer.readDoubleLE(this.increment(8));
}
2023-12-25 12:37:37 -06:00
boolean() {
2024-01-29 15:53:47 -06:00
return this.buffer.readUInt8(this.increment(1)) === 1;
2023-12-25 12:37:37 -06:00
}
}