import type { BigInteger } from "big-integer" ; import { BigIntHelper } from "./bigIntHelper" ; import { Converter } from "./converter" ; /**
 * Keep track of the write index within a stream.
 */ export class WriteStream { internal private static readonly CHUNK_SIZE : number = 4096 ; internal private _storage : Uint8Array ; internal private _writeIndex : number ; /**
     * Create a new instance of ReadStream.
     */ constructor ( ) { this . _storage = new Uint8Array ( WriteStream . CHUNK_SIZE ) ; this . _writeIndex = 0 ; } returns public length ( ) { return this . _storage . length ; } returns public unused ( ) { return this . _storage . length - this . _writeIndex ; } returns public finalBytes ( ) { return this . _storage . subarray ( 0 , this . _writeIndex ) ; } returns public finalHex ( ) { return Converter . bytesToHex ( this . _storage . subarray ( 0 , this . _writeIndex ) ) ; } returns public getWriteIndex ( ) { return this . _writeIndex ; } param writeIndex public setWriteIndex ( writeIndex ) { this . _writeIndex = writeIndex ; if ( writeIndex >= this . _storage . length ) { throw new Error ( `You cannot set the writeIndex to ${ writeIndex } as the stream is only ${ this . _storage . length } in length` ) ; } } param name param length param val public writeFixedHex ( name , length , val ) { if ( ! Converter . isHex ( val ) ) { throw new Error ( `The ${ name } should be in hex format` ) ; } if ( length * 2 !== val . length ) { throw new Error ( `${ name } length ${ val . length } does not match expected length ${ length * 2 }` ) ; } this . expand ( length ) ; this . _storage . set ( Converter . hexToBytes ( val ) , this . _writeIndex ) ; this . _writeIndex += length ; } param name param length param val public writeBytes ( name , length , val ) { this . expand ( length ) ; this . _storage . set ( val , this . _writeIndex ) ; this . _writeIndex += length ; } param name param val public writeByte ( name , val ) { this . expand ( 1 ) ; this . _storage [ this . _writeIndex ++ ] = val & 0xff ; } param name param val public writeUInt16 ( name , val ) { this . expand ( 2 ) ; this . _storage [ this . _writeIndex ++ ] = val & 0xff ; this . _storage [ this . _writeIndex ++ ] = val >>> 8 ; } param name param val public writeUInt32 ( name , val ) { this . expand ( 4 ) ; this . _storage [ this . _writeIndex ++ ] = val & 0xff ; this . _storage [ this . _writeIndex ++ ] = val >>> 8 ; this . _storage [ this . _writeIndex ++ ] = val >>> 16 ; this . _storage [ this . _writeIndex ++ ] = val >>> 24 ; } param name param val public writeUInt64 ( name , val ) { this . expand ( 8 ) ; BigIntHelper . write8 ( val , this . _storage , this . _writeIndex ) ; this . _writeIndex += 8 ; } param name param val public writeBoolean ( name , val ) { this . expand ( 1 ) ; this . _storage [ this . _writeIndex ++ ] = val ? 1 : 0 ; } param additional private expand ( additional ) { if ( this . _writeIndex + additional > this . _storage . byteLength ) { const newArr = new Uint8Array ( this . _storage . length + Math . ceil ( additional / WriteStream . CHUNK_SIZE ) * WriteStream . CHUNK_SIZE ) ; newArr . set ( this . _storage , 0 ) ; this . _storage = newArr ; } } }