Last active 1741880600

Revision aa75748f8e7bd8f7d8fc06b08dfba6b8ec460a29

BufferReader.ts Raw
1export default class BufferReader {
2 #buffer: Buffer
3 #offset: number
4 #length = 0
5
6 static *getBuffers(data: Buffer) {
7 let i = 0
8 let reader = new BufferReader(data)
9 yield reader
10 while(reader.nextBuffer != null) {
11 console.log(i++, "got next buffer", reader.nextBuffer.toString('hex'))
12 reader = new BufferReader(reader.nextBuffer)
13 yield reader
14 }
15 return null
16 }
17
18 constructor(data: Buffer) {
19 if(!data) throw new Error("buffer is null or undefined")
20 console.log(data.toString('hex'))
21 this.#buffer = data
22 this.#offset = 0
23 this.#length = data.byteLength
24 }
25
26 get offset() {
27 return this.#offset
28 }
29
30 get length() {
31 return this.#length
32 }
33
34 get isEOF() {
35 return this.#offset >= this.#length || this.#curChar == 0xa // new line
36 }
37
38 get IsRecordSeparator() {
39 return this.#curChar === BufferReader.RecordSeparator
40 }
41
42 get #curChar() {
43 return this.#buffer.at(this.#offset)
44 }
45
46 static get RecordSeparator() {
47 return 0x1e
48 }
49
50 peekByte(): number {
51 return this.#buffer.readInt8(this.#offset)
52 }
53
54 readByte(): number {
55 const value = this.#buffer.readInt8(this.#offset)
56 this.#offset += 1
57 return value
58 }
59
60 readShort(): number {
61 const value = this.#buffer.readInt16LE(this.#offset)
62 this.#offset += 2
63 return value
64 }
65
66 readInt(): number {
67 const value = this.#buffer.readInt32LE(this.#offset)
68 this.#offset += 4
69 return value
70 }
71
72 readFloat(): number {
73 const value = this.#buffer.readFloatLE(this.#offset)
74 this.#offset += 4
75 return value
76 }
77
78 readString(length: number): string {
79 const slice = this.#buffer.subarray(this.#offset, this.#offset + length)
80 this.#offset += length
81 return slice.toString().split("\0").shift();
82 }
83
84 readVarString(): string {
85 let length = this.readShort()
86 return this.readString(length)
87 }
88
89 *readRecord() {
90 let recordStart: number = null
91 while(!this.isEOF) {
92 if(this.IsRecordSeparator) {
93 if(recordStart != null) {
94 // End of record
95 yield this.#buffer.subarray(recordStart, this.#offset)
96 }
97 recordStart = this.#offset + 1
98 }
99 this.#offset++;
100 }
101 // Check to see that we have seen the \n(0xa) character at the end, otherwise this record got truncated:
102 if(recordStart != null && this.#curChar == 0xa) {
103 yield this.#buffer.subarray(recordStart, this.#offset)
104 }
105 return null
106 }
107
108 get nextBuffer() {
109 if(this.#curChar == 0xa && this.#offset + 1 < this.#buffer.length) {
110 return this.#buffer.subarray(this.#offset + 1)
111 } else {
112 return null
113 }
114 }
115
116 at(i: number): number {
117 return this.#buffer.at(i)
118 }
119
120 next(): number | null {
121 if(this.#offset == this.#length) return null
122 return this.readByte()
123 }
124 }