main
|
Last change
on this file was 62b2964, checked in by Klimentina Efremova <klimentina08642@…>, 2 weeks ago |
|
Project Handcraft Marketplace
|
-
Property mode
set to
100644
|
|
File size:
1.4 KB
|
| Line | |
|---|
| 1 | export class BufferReader {
|
|---|
| 2 | private buffer: Buffer = Buffer.allocUnsafe(0)
|
|---|
| 3 |
|
|---|
| 4 | // TODO(bmc): support non-utf8 encoding?
|
|---|
| 5 | private encoding: BufferEncoding = 'utf-8'
|
|---|
| 6 |
|
|---|
| 7 | constructor(private offset: number = 0) {}
|
|---|
| 8 |
|
|---|
| 9 | public setBuffer(offset: number, buffer: Buffer): void {
|
|---|
| 10 | this.offset = offset
|
|---|
| 11 | this.buffer = buffer
|
|---|
| 12 | }
|
|---|
| 13 |
|
|---|
| 14 | public int16(): number {
|
|---|
| 15 | const result = this.buffer.readInt16BE(this.offset)
|
|---|
| 16 | this.offset += 2
|
|---|
| 17 | return result
|
|---|
| 18 | }
|
|---|
| 19 |
|
|---|
| 20 | public byte(): number {
|
|---|
| 21 | const result = this.buffer[this.offset]
|
|---|
| 22 | this.offset++
|
|---|
| 23 | return result
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | public int32(): number {
|
|---|
| 27 | const result = this.buffer.readInt32BE(this.offset)
|
|---|
| 28 | this.offset += 4
|
|---|
| 29 | return result
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public uint32(): number {
|
|---|
| 33 | const result = this.buffer.readUInt32BE(this.offset)
|
|---|
| 34 | this.offset += 4
|
|---|
| 35 | return result
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public string(length: number): string {
|
|---|
| 39 | const result = this.buffer.toString(this.encoding, this.offset, this.offset + length)
|
|---|
| 40 | this.offset += length
|
|---|
| 41 | return result
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | public cstring(): string {
|
|---|
| 45 | const start = this.offset
|
|---|
| 46 | let end = start
|
|---|
| 47 | // eslint-disable-next-line no-empty
|
|---|
| 48 | while (this.buffer[end++]) {}
|
|---|
| 49 | this.offset = end
|
|---|
| 50 | return this.buffer.toString(this.encoding, start, end - 1)
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public bytes(length: number): Buffer {
|
|---|
| 54 | const result = this.buffer.slice(this.offset, this.offset + length)
|
|---|
| 55 | this.offset += length
|
|---|
| 56 | return result
|
|---|
| 57 | }
|
|---|
| 58 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.