Ejemplo n.º 1
0
    def testBufferConstructor(self):
        byteSize = 2
        bitBuffer = BitBuffer(bytes([1, 2]))
        self.assertEqual(8 * byteSize, bitBuffer.getBitSize())

        emptyBitSize = 0
        emptyBitBuffer = BitBuffer(bytes([]))
        self.assertEqual(emptyBitSize, emptyBitBuffer.getBitSize())
Ejemplo n.º 2
0
    def testBufferBitSizeConstructor(self):
        bitSize = 11
        bitBuffer = BitBuffer(bytes([1, 2]), bitSize)
        self.assertEqual(bitSize, bitBuffer.getBitSize())

        emptyBitSize = 0
        emptyBitBuffer = BitBuffer(bytes([]), emptyBitSize)
        self.assertEqual(emptyBitSize, emptyBitBuffer.getBitSize())

        outOfRangeBitSize = 9
        with self.assertRaises(PythonRuntimeException):
            BitBuffer([1], outOfRangeBitSize)  # throws!
Ejemplo n.º 3
0
    def writeBitBuffer(self, bitBuffer: BitBuffer) -> None:
        """
        Writes a bit buffer to the underlying storage. Length of the bit buffer is written as varuint64
        at the beginning.

        :param bitBuffer: Bit buffer to write.
        """

        bitSize = bitBuffer.getBitSize()
        self.writeVarSize(bitSize)

        writeBuffer = bitBuffer.getBuffer()
        numBytesToWrite = bitSize // 8
        numRestBits = bitSize - numBytesToWrite * 8
        beginBitPosition = self._bitPosition
        if (beginBitPosition & 0x07) != 0:
            # we are not aligned to byte
            for i in range(numBytesToWrite):
                self.writeBits(writeBuffer[i], 8)
        else:
            # we are aligned to byte
            self._byteArray += writeBuffer[0:numBytesToWrite]
            self._bitPosition += numBytesToWrite * 8

        if numRestBits > 0:
            self.writeBits(writeBuffer[numBytesToWrite] >> (8 - numRestBits),
                           numRestBits)
Ejemplo n.º 4
0
 def testFromBitBuffer(self):
     bitBuffer = BitBuffer(bytes([0xAE, 0xEA, 0x80]), 17)
     reader = BitStreamReader.fromBitBuffer(bitBuffer)
     self.assertEqual(bitBuffer.getBitSize(), reader.getBufferBitSize())
     self.assertEqual(0xAEE, reader.readBits(12))
     self.assertEqual(0x0A, reader.readBits(4))
     self.assertEqual(0x01, reader.readBits(1))
     with self.assertRaises(PythonRuntimeException):
         reader.readBits(1)
Ejemplo n.º 5
0
    def fromBitBuffer(cls: typing.Type['BitStreamReader'], bitBuffer: BitBuffer) -> 'BitStreamReader':
        """
        Constructs bit stream reader from bit buffer.

        :param bitBuffer: Bit buffer to read as a bit stream.
        """

        instance = cls(bitBuffer.getBuffer())
        instance._bitSize = bitBuffer.getBitSize()

        return instance
Ejemplo n.º 6
0
def getBitSizeOfBitBuffer(bitBuffer: BitBuffer) -> int:
    """
    Gets the bit size of bit buffer which is stored in bit stream.

    :param bitBuffer: Bit buffer for calculation.
    :returns: Length of bit buffer in bits.
    :raises PythonRuntimeException: Throws if given bit buffer is too long.
    """
    bitBufferSize = bitBuffer.getBitSize()

    # bit buffer consists of varsize for bit size followed by the bits
    return getBitSizeOfVarSize(bitBufferSize) + bitBufferSize
Ejemplo n.º 7
0
 def testGetBitSize(self):
     bitSize = 11
     bitBuffer = BitBuffer(bytes([0xAB, 0x07]), bitSize)
     self.assertEqual(bitSize, bitBuffer.getBitSize())
Ejemplo n.º 8
0
 def testFromBitBufferOverflow(self):
     bitBuffer = BitBuffer(bytes([0xFF, 0xFF, 0xF0]), 19)
     reader = BitStreamReader.fromBitBuffer(bitBuffer)
     self.assertEqual(bitBuffer.getBitSize(), reader.getBufferBitSize())
     with self.assertRaises(PythonRuntimeException):
         reader.readBits(20)