A raw binary stream subclass with memoryview-like interface to a :class:`.GettableBase`-derived object. Args: raw (:class:`.GettableFile`/:class:`.GettableMemory`/:class:`.GettableBase`-derived): A :class:`.GettableBase`-derived object that (essentially) supports
| 238 | |
| 239 | |
| 240 | class FileView(io.RawIOBase): |
| 241 | """A raw binary stream subclass with memoryview-like interface to a |
| 242 | :class:`.GettableBase`-derived object. |
| 243 | |
| 244 | Args: |
| 245 | raw (:class:`.GettableFile`/:class:`.GettableMemory`/:class:`.GettableBase`-derived): |
| 246 | A :class:`.GettableBase`-derived object that (essentially) supports |
| 247 | efficient and thread-safe `getinto` operation. |
| 248 | |
| 249 | Note: |
| 250 | Although similar to :mod:`mmap` for files, :class:`.FileView` provides: |
| 251 | - a unified interface to *both* files and memory objects |
| 252 | - a way to ensure file "seek & read" operation is atomic (thread-safe) |
| 253 | via the :class:`.Gettable` layer |
| 254 | |
| 255 | Note: |
| 256 | Use the slice syntax (item getter) to retrieve an isolated file segment |
| 257 | view (a new instance of :class:`.FileView` that references the same |
| 258 | underlying data, but supports independent read operations). |
| 259 | """ |
| 260 | |
| 261 | def __init__(self, raw): |
| 262 | super().__init__() |
| 263 | self._raw = raw |
| 264 | self._pos = 0 |
| 265 | self._offset = 0 |
| 266 | self._size = len(raw) |
| 267 | |
| 268 | def seek(self, pos, whence=os.SEEK_SET): |
| 269 | if whence == os.SEEK_SET: |
| 270 | self._pos = pos |
| 271 | elif whence == os.SEEK_CUR: |
| 272 | self._pos += pos |
| 273 | elif whence == os.SEEK_END: |
| 274 | self._pos = self._size + pos |
| 275 | else: |
| 276 | raise ValueError("whence must be one of 'io.SEEK_{SET,CUR,END}'") |
| 277 | |
| 278 | return self._pos |
| 279 | |
| 280 | def tell(self): |
| 281 | return self._pos |
| 282 | |
| 283 | def readinto(self, b): |
| 284 | """Read bytes into a pre-allocated bytes-like object b. |
| 285 | |
| 286 | Returns: |
| 287 | int: |
| 288 | The number of bytes read. |
| 289 | """ |
| 290 | start = self._offset + self._pos |
| 291 | stop = self._offset + self._size |
| 292 | key = slice(start, stop) |
| 293 | n = self._raw.getinto(key, b) |
| 294 | self._pos += n |
| 295 | return n |
| 296 | |
| 297 | def __len__(self): |
no outgoing calls