import Exception from './../Exception' ; author Sean Owen
  export default class BitSource { private byteOffset : number ; private bitOffset : number ; param bytes public constructor ( private bytes ) { this . byteOffset = 0 ; this . bitOffset = 0 ; } return index of next bit in current byte which would be read by the next call to  {@link #readBits(int)} .
      public getBitOffset ( ) { return this . bitOffset ; } return index of next byte in input byte array which would be read by the next call to  {@link #readBits(int)} .
      public getByteOffset ( ) { return this . byteOffset ; } param numBits return throws public readBits ( numBits ) { if ( numBits < 1 || numBits > 32 || numBits > this . available ( ) ) { throw new Exception ( Exception . IllegalArgumentException , "" + numBits ) ; } let result = 0 ; let bitOffset = this . bitOffset ; let byteOffset = this . byteOffset ; const bytes = this . bytes ; if ( bitOffset > 0 ) { const bitsLeft = 8 - bitOffset ; const toRead = numBits < bitsLeft ? numBits : bitsLeft ; const bitsToNotRead = bitsLeft - toRead ; const mask = ( 0xFF >> ( 8 - toRead ) ) << bitsToNotRead ; result = ( bytes [ byteOffset ] & mask ) >> bitsToNotRead ; numBits -= toRead ; bitOffset += toRead ; if ( bitOffset == 8 ) { bitOffset = 0 ; byteOffset ++ ; } } if ( numBits > 0 ) { while ( numBits >= 8 ) { result = ( result << 8 ) | ( bytes [ byteOffset ] & 0xFF ) ; byteOffset ++ ; numBits -= 8 ; } if ( numBits > 0 ) { const bitsToNotRead = 8 - numBits ; const mask = ( 0xFF >> bitsToNotRead ) << bitsToNotRead ; result = ( result << numBits ) | ( ( bytes [ byteOffset ] & mask ) >> bitsToNotRead ) ; bitOffset += numBits ; } } this . bitOffset = bitOffset ; this . byteOffset = byteOffset ; return result ; } return public available ( ) { return 8 * ( this . bytes . length - this . byteOffset ) - this . bitOffset ; } }