class constructor param buffer export class BufferReader { private readonly _buffer : Buffer ; private readonly _dataView : DataView ; private _offset : number ; private _bitpos : number ; private _byte : number ; constructor ( buffer ) { this . _buffer = buffer ; this . _dataView = new DataView ( this . _buffer ) ; this . _offset = 0 ; this . _bitpos = 0 ; this . _byte = 0 ; } method param offset return readUint8 ( offset ) { return this . _dataView . getUint8 ( offset ) ; } method param offset return readUint16 ( offset ) { return this . _dataView . getUint16 ( offset ) ; } method param offset return readUint32 ( offset ) { return this . _dataView . getUint32 ( offset ) ; } method return { Number } readNext ( ) { const value = this . readUint8 ( this . _offset ) ; this . _offset += 1 ; return value ; } readBits ( length ) { if ( length > 32 || length === 0 ) { throw new Error ( 'length has to be between 0 - 31 bits' ) ; } let result = 0 ; for ( let i = 1 ; i <= length ; ++ i ) { if ( this . _bitpos === 0 ) { this . _byte = this . readNext ( ) ; } result = ( result << 1 ) | ( ( this . _byte >> ( 8 - ++ this . _bitpos ) ) & 0x01 ) ; this . _bitpos %= 8 ; } return result ; } readUnsignedExpGolomb ( ) { let bitsToRead = 0 ; while ( this . readBits ( 1 ) !== 1 ) { bitsToRead ++ ; } if ( bitsToRead === 0 ) { return 0 ; } if ( bitsToRead >= 31 ) { throw new Error ( 'read unsigned exponential Golomb: internal error' ) ; } let n = this . readBits ( bitsToRead ) ; n |= 0x1 << bitsToRead ; return n - 1 ; } readSignedExpGolomb ( ) { let r = this . readUnsignedExpGolomb ( ) ; if ( r & 0x01 ) { r = ( r + 1 ) >> 1 ; } else { r = - ( r >> 1 ) ; } return r ; } method return { Number } size ( ) { return this . _buffer . byteLength ; } method return { Uint8Array } getUint8Array ( ) { return new Uint8Array ( this . _buffer ) ; } method return { ArrayBuffer } getArrayBuffer ( ) { return this . _buffer ; } }