BufferReader.ts
· 3.0 KiB · TypeScript
Raw
export default class BufferReader {
#buffer: Buffer
#offset: number
#length = 0
static *getBuffers(data: Buffer) {
let i = 0
let reader = new BufferReader(data)
yield reader
while(reader.nextBuffer != null) {
console.log(i++, "got next buffer", reader.nextBuffer.toString('hex'))
reader = new BufferReader(reader.nextBuffer)
yield reader
}
return null
}
constructor(data: Buffer) {
if(!data) throw new Error("buffer is null or undefined")
console.log(data.toString('hex'))
this.#buffer = data
this.#offset = 0
this.#length = data.byteLength
}
get offset() {
return this.#offset
}
get length() {
return this.#length
}
get isEOF() {
return this.#offset >= this.#length || this.#curChar == 0xa // new line
}
get IsRecordSeparator() {
return this.#curChar === BufferReader.RecordSeparator
}
get #curChar() {
return this.#buffer.at(this.#offset)
}
static get RecordSeparator() {
return 0x1e
}
peekByte(): number {
return this.#buffer.readInt8(this.#offset)
}
readByte(): number {
const value = this.#buffer.readInt8(this.#offset)
this.#offset += 1
return value
}
readShort(): number {
const value = this.#buffer.readInt16LE(this.#offset)
this.#offset += 2
return value
}
readInt(): number {
const value = this.#buffer.readInt32LE(this.#offset)
this.#offset += 4
return value
}
readFloat(): number {
const value = this.#buffer.readFloatLE(this.#offset)
this.#offset += 4
return value
}
readString(length: number): string {
const slice = this.#buffer.subarray(this.#offset, this.#offset + length)
this.#offset += length
return slice.toString().split("\0").shift();
}
readVarString(): string {
let length = this.readShort()
return this.readString(length)
}
*readRecord() {
let recordStart: number = null
while(!this.isEOF) {
if(this.IsRecordSeparator) {
if(recordStart != null) {
// End of record
yield this.#buffer.subarray(recordStart, this.#offset)
}
recordStart = this.#offset + 1
}
this.#offset++;
}
// Check to see that we have seen the \n(0xa) character at the end, otherwise this record got truncated:
if(recordStart != null && this.#curChar == 0xa) {
yield this.#buffer.subarray(recordStart, this.#offset)
}
return null
}
get nextBuffer() {
if(this.#curChar == 0xa && this.#offset + 1 < this.#buffer.length) {
return this.#buffer.subarray(this.#offset + 1)
} else {
return null
}
}
at(i: number): number {
return this.#buffer.at(i)
}
next(): number | null {
if(this.#offset == this.#length) return null
return this.readByte()
}
}
| 1 | export 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 | } |