mirror of
https://github.com/blitzkit/blitzkit.git
synced 2026-10-21 10:47:04 +02:00
feat: write stream
This commit is contained in:
1 parent
93e405c370
commit
2951ccdad5
3 files changed
+76
-5
No files matched your search
@@ -71,3 +71,76 @@ export class ReadStream {
|
||||
return this.buffer.readUInt8(this.increment(1)) === 1;
|
||||
}
|
||||
}
|
||||
|
||||
export class WriteStream {
|
||||
private array: number[] = [];
|
||||
|
||||
get buffer() {
|
||||
return Buffer.from(this.array);
|
||||
}
|
||||
|
||||
constructor(buffer?: Buffer) {
|
||||
if (buffer) this.array = Array.from(buffer);
|
||||
}
|
||||
|
||||
byte(value: Uint8Array) {
|
||||
this.array.push(...value);
|
||||
return this;
|
||||
}
|
||||
|
||||
ascii(value: string) {
|
||||
for (let index = 0; index < value.length; index++) {
|
||||
this.array.push(value.charCodeAt(index));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
private uintN(bytes: number, value: number) {
|
||||
for (let index = 0; index < bytes; index++) {
|
||||
this.array.push((value >>> (8 * index)) & 0xff);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
uint8(value: number) {
|
||||
this.uintN(1, value);
|
||||
return this;
|
||||
}
|
||||
uint16(value: number) {
|
||||
this.uintN(2, value);
|
||||
return this;
|
||||
}
|
||||
uint32(value: number) {
|
||||
this.uintN(4, value);
|
||||
return this;
|
||||
}
|
||||
uint64(value: bigint) {
|
||||
for (let index = 0n; index < 8n; index++) {
|
||||
this.uint8(Number((value >> (8n * index)) & 0xffn));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
private intN(bytes: number, value: number) {
|
||||
for (let index = 0; index < bytes; index++) {
|
||||
this.array.push((value >> (8 * index)) & 0xff);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
int8(value: number) {
|
||||
this.intN(1, value);
|
||||
return this;
|
||||
}
|
||||
int16(value: number) {
|
||||
this.intN(2, value);
|
||||
return this;
|
||||
}
|
||||
int32(value: number) {
|
||||
this.intN(4, value);
|
||||
return this;
|
||||
}
|
||||
int64(value: bigint) {
|
||||
for (let index = 0n; index < 8n; index++) {
|
||||
this.int8(Number((value >> (8n * index)) & 0xffn));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user