Reads `count` bits and returns an uint, MSB read first. May raise BitReaderError if not enough data could be read or IOError by the underlying file object.
(self, count: int)
| 969 | self._pos = fileobj.tell() |
| 970 | |
| 971 | def bits(self, count: int) -> int: |
| 972 | """Reads `count` bits and returns an uint, MSB read first. |
| 973 | |
| 974 | May raise BitReaderError if not enough data could be read or |
| 975 | IOError by the underlying file object. |
| 976 | """ |
| 977 | |
| 978 | if count < 0: |
| 979 | raise ValueError |
| 980 | |
| 981 | if count > self._bits: |
| 982 | n_bytes = (count - self._bits + 7) // 8 |
| 983 | data = self._fileobj.read(n_bytes) |
| 984 | if len(data) != n_bytes: |
| 985 | raise BitReaderError("not enough data") |
| 986 | for b in bytearray(data): |
| 987 | self._buffer = (self._buffer << 8) | b |
| 988 | self._bits += n_bytes * 8 |
| 989 | |
| 990 | self._bits -= count |
| 991 | value = self._buffer >> self._bits |
| 992 | self._buffer &= (1 << self._bits) - 1 |
| 993 | assert self._bits < 8 |
| 994 | return value |
| 995 | |
| 996 | def bytes(self, count: int) -> bytes: |
| 997 | """Returns a bytearray of length `count`. Works unaligned.""" |