Reads `count` bits and returns an uint, LSB read first. May raise BitReaderError if not enough data could be read or IOError by the underlying file object.
(self, count)
| 91 | return value |
| 92 | |
| 93 | def bits(self, count): |
| 94 | """Reads `count` bits and returns an uint, LSB read first. |
| 95 | |
| 96 | May raise BitReaderError if not enough data could be read or |
| 97 | IOError by the underlying file object. |
| 98 | """ |
| 99 | if count < 0: |
| 100 | raise ValueError |
| 101 | |
| 102 | value = 0 |
| 103 | if count <= self._bits: |
| 104 | value = self._lsb(count) |
| 105 | else: |
| 106 | # First read all available bits |
| 107 | shift = 0 |
| 108 | remaining = count |
| 109 | if self._bits > 0: |
| 110 | remaining -= self._bits |
| 111 | shift = self._bits |
| 112 | value = self._lsb(self._bits) |
| 113 | assert self._bits == 0 |
| 114 | |
| 115 | # Now add additional bytes |
| 116 | n_bytes = (remaining - self._bits + 7) // 8 |
| 117 | data = self._fileobj.read(n_bytes) |
| 118 | if len(data) != n_bytes: |
| 119 | raise BitReaderError("not enough data") |
| 120 | for b in bytearray(data): |
| 121 | if remaining > 8: # Use full byte |
| 122 | remaining -= 8 |
| 123 | value = (b << shift) | value |
| 124 | shift += 8 |
| 125 | else: |
| 126 | self._buffer = b |
| 127 | self._bits = 8 |
| 128 | b = self._lsb(remaining) |
| 129 | value = (b << shift) | value |
| 130 | |
| 131 | assert 0 <= self._bits < 8 |
| 132 | return value |
| 133 | |
| 134 | |
| 135 | class TAKHeaderError(error): |
no test coverage detected