Read the number of bytes requested. This function should be used when bytes "should" be present for reading. If the bytes are truly not available (due to EOF), then the IncompleteRead exception can be used to detect the problem.
(self, amt)
| 641 | raise IncompleteRead(bytes(b[0:total_bytes])) |
| 642 | |
| 643 | def _safe_read(self, amt): |
| 644 | """Read the number of bytes requested. |
| 645 | |
| 646 | This function should be used when <amt> bytes "should" be present for |
| 647 | reading. If the bytes are truly not available (due to EOF), then the |
| 648 | IncompleteRead exception can be used to detect the problem. |
| 649 | """ |
| 650 | cursize = min(amt, _MIN_READ_BUF_SIZE) |
| 651 | data = self.fp.read(cursize) |
| 652 | if len(data) >= amt: |
| 653 | return data |
| 654 | if len(data) < cursize: |
| 655 | raise IncompleteRead(data, amt - len(data)) |
| 656 | |
| 657 | data = io.BytesIO(data) |
| 658 | data.seek(0, 2) |
| 659 | while True: |
| 660 | # This is a geometric increase in read size (never more than |
| 661 | # doubling out the current length of data per loop iteration). |
| 662 | delta = min(cursize, amt - cursize) |
| 663 | data.write(self.fp.read(delta)) |
| 664 | if data.tell() >= amt: |
| 665 | return data.getvalue() |
| 666 | cursize += delta |
| 667 | if data.tell() < cursize: |
| 668 | raise IncompleteRead(data.getvalue(), amt - data.tell()) |
| 669 | |
| 670 | def _safe_readinto(self, b): |
| 671 | """Same as _safe_read, but for reading into a buffer.""" |