Reads from the file at offset for length.
(self, offset: int, length: int, pad: bool = False)
| 153 | ) |
| 154 | |
| 155 | def read(self, offset: int, length: int, pad: bool = False) -> bytes: |
| 156 | """Reads from the file at offset for length.""" |
| 157 | if not self.is_valid(offset, length): |
| 158 | invalid_address = offset |
| 159 | if self.minimum_address < offset <= self.maximum_address: |
| 160 | invalid_address = self.maximum_address + 1 |
| 161 | raise exceptions.InvalidAddressException( |
| 162 | self.name, invalid_address, "Offset outside of the buffer boundaries" |
| 163 | ) |
| 164 | |
| 165 | # TODO: implement locking for multi-threading |
| 166 | with self._lock: |
| 167 | self._file.seek(offset) |
| 168 | data = self._file.read(length) |
| 169 | |
| 170 | if len(data) < length: |
| 171 | if pad: |
| 172 | data += b"\x00" * (length - len(data)) |
| 173 | else: |
| 174 | raise exceptions.InvalidAddressException( |
| 175 | self.name, |
| 176 | offset + len(data), |
| 177 | "Could not read sufficient bytes from the " + self.name + " file", |
| 178 | ) |
| 179 | return data |
| 180 | |
| 181 | def write(self, offset: int, data: bytes) -> None: |
| 182 | """Writes to the file. |