Write the given buffer to the IO stream. Returns the number of bytes written, which may be less than len(b).
(self, b)
| 319 | |
| 320 | # def write(self, b: bytes) -> int: |
| 321 | def write(self, b): |
| 322 | """Write the given buffer to the IO stream. |
| 323 | |
| 324 | Returns the number of bytes written, which may be less than |
| 325 | len(b). |
| 326 | |
| 327 | """ |
| 328 | self._check_closed() |
| 329 | self._checkWritable() |
| 330 | |
| 331 | if isinstance(b, str): |
| 332 | raise TypeError("can't write str to binary stream") |
| 333 | |
| 334 | n = len(b) |
| 335 | if n == 0: |
| 336 | return 0 |
| 337 | |
| 338 | pos = self._pos |
| 339 | |
| 340 | # Is the pointer beyond the real end of data? |
| 341 | end2off = pos - self._node.nrows |
| 342 | if end2off > 0: |
| 343 | # Zero-fill the gap between the end of data and the pointer. |
| 344 | self._append_zeros(end2off) |
| 345 | |
| 346 | # Append data. |
| 347 | self._node.append( |
| 348 | np.ndarray(buffer=b, dtype=self._vtype, shape=self._vshape(n)) |
| 349 | ) |
| 350 | |
| 351 | self._pos += n |
| 352 | |
| 353 | return n |
| 354 | |
| 355 | def _check_closed(self): |
| 356 | """Check if file node is open. |