| 8 | * type moves the offset by the size of the type read. |
| 9 | */ |
| 10 | export class BinaryReader { |
| 11 | /** |
| 12 | * The underlying `ArrayBuffer` instance that contains the binary data. |
| 13 | */ |
| 14 | public readonly buffer: ArrayBuffer; |
| 15 | |
| 16 | /** |
| 17 | * The `DataView` instance that provides methods to read data from the `ArrayBuffer`. |
| 18 | */ |
| 19 | public readonly view: DataView; |
| 20 | |
| 21 | /** |
| 22 | * The byte order used for reading binary data. |
| 23 | */ |
| 24 | public readonly byteOrder: ByteOrder; |
| 25 | |
| 26 | /** |
| 27 | * A boolean value indicating whether the byte order is little-endian. |
| 28 | */ |
| 29 | public readonly isLittleEndian: boolean; |
| 30 | |
| 31 | private offset: number = 0; |
| 32 | |
| 33 | constructor(buffer: ArrayBuffer, byteOrder: ByteOrder) { |
| 34 | this.buffer = buffer; |
| 35 | this.view = new DataView(buffer); |
| 36 | this.byteOrder = byteOrder; |
| 37 | this.isLittleEndian = byteOrder === ByteOrder.LittleEndian; |
| 38 | } |
| 39 | |
| 40 | /** |
| 41 | * A boolean value indicating whether the reader has reached the end of the binary data. |
| 42 | */ |
| 43 | get isEmpty(): boolean { |
| 44 | return this.offset + 1 >= this.view.byteLength; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * The current offset in the binary data. |
| 49 | */ |
| 50 | get currentOffset(): number { |
| 51 | return this.offset; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Moves the internal offset forward by the specified number. |
| 56 | * |
| 57 | * @param n - The number of positions to move the offset forward. |
| 58 | * @return The new position of the internal offset after being moved. |
| 59 | */ |
| 60 | skip(n: number) { |
| 61 | this.offset += n; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Skips the binary data until the specified byte value is found. |
| 66 | */ |
| 67 | skipUntilByte(value: number) { |
nothing calls this directly
no outgoing calls
no test coverage detected