import type { BigInteger } from "big-integer" ; import { BigIntHelper } from "./bigIntHelper" ; import { Converter } from "./converter" ; /**
 * Keep track of the read index within a stream.
 */ export class ReadStream { internal private readonly _storage : Uint8Array ; internal private _readIndex : number ; param storage param readStartIndex constructor ( storage , readStartIndex = 0 ) { this . _storage = new Uint8Array ( storage ) ; this . _readIndex = readStartIndex ; } returns public length ( ) { return this . _storage . byteLength ; } param remaining returns public hasRemaining ( remaining ) { return this . _readIndex + remaining <= this . _storage . byteLength ; } returns public unused ( ) { return this . _storage . byteLength - this . _readIndex ; } returns public getReadIndex ( ) { return this . _readIndex ; } param readIndex public setReadIndex ( readIndex ) { this . _readIndex = readIndex ; if ( readIndex >= this . _storage . length ) { throw new Error ( `You cannot set the readIndex to ${ readIndex } as the stream is only ${ this . _storage . length } in length` ) ; } } param name param length param moveIndex returns public readFixedHex ( name , length , moveIndex = true ) { if ( ! this . hasRemaining ( length ) ) { throw new Error ( `${ name } length ${ length } exceeds the remaining data ${ this . unused ( ) }` ) ; } const hex = Converter . bytesToHex ( this . _storage , this . _readIndex , length ) ; if ( moveIndex ) { this . _readIndex += length ; } return hex ; } param name param length param moveIndex returns public readBytes ( name , length , moveIndex = true ) { if ( ! this . hasRemaining ( length ) ) { throw new Error ( `${ name } length ${ length } exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = this . _storage . slice ( this . _readIndex , this . _readIndex + length ) ; if ( moveIndex ) { this . _readIndex += length ; } return val ; } param name param moveIndex returns public readByte ( name , moveIndex = true ) { if ( ! this . hasRemaining ( 1 ) ) { throw new Error ( `${ name } length 1 exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = this . _storage [ this . _readIndex ] ; if ( moveIndex ) { this . _readIndex += 1 ; } return val ; } param name param moveIndex returns public readUInt16 ( name , moveIndex = true ) { if ( ! this . hasRemaining ( 2 ) ) { throw new Error ( `${ name } length 2 exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = this . _storage [ this . _readIndex ] | ( this . _storage [ this . _readIndex + 1 ] << 8 ) ; if ( moveIndex ) { this . _readIndex += 2 ; } return val ; } param name param moveIndex returns public readUInt32 ( name , moveIndex = true ) { if ( ! this . hasRemaining ( 4 ) ) { throw new Error ( `${ name } length 4 exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = this . _storage [ this . _readIndex ] | ( this . _storage [ this . _readIndex + 1 ] * 0x100 ) | ( this . _storage [ this . _readIndex + 2 ] * 0x10000 + this . _storage [ this . _readIndex + 3 ] * 0x1000000 ) ; if ( moveIndex ) { this . _readIndex += 4 ; } return val ; } param name param moveIndex returns public readUInt64 ( name , moveIndex = true ) { if ( ! this . hasRemaining ( 8 ) ) { throw new Error ( `${ name } length 8 exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = BigIntHelper . read8 ( this . _storage , this . _readIndex ) ; if ( moveIndex ) { this . _readIndex += 8 ; } return val ; } param name param moveIndex returns public readBoolean ( name , moveIndex = true ) { if ( ! this . hasRemaining ( 1 ) ) { throw new Error ( `${ name } length 1 exceeds the remaining data ${ this . unused ( ) }` ) ; } const val = this . _storage [ this . _readIndex ] ; if ( moveIndex ) { this . _readIndex += 1 ; } return val !== 0 ; } }