Read exactly `n` bytes. Raise an IncompleteReadError if EOF is reached before `n` bytes can be read. The IncompleteReadError.partial attribute of the exception will contain the partial read bytes. if n is zero, return empty bytes object. Returned value is n
(self, n)
| 737 | return data |
| 738 | |
| 739 | async def readexactly(self, n): |
| 740 | """Read exactly `n` bytes. |
| 741 | |
| 742 | Raise an IncompleteReadError if EOF is reached before `n` bytes can be |
| 743 | read. The IncompleteReadError.partial attribute of the exception will |
| 744 | contain the partial read bytes. |
| 745 | |
| 746 | if n is zero, return empty bytes object. |
| 747 | |
| 748 | Returned value is not limited with limit, configured at stream |
| 749 | creation. |
| 750 | |
| 751 | If stream was paused, this function will automatically resume it if |
| 752 | needed. |
| 753 | """ |
| 754 | if n < 0: |
| 755 | raise ValueError('readexactly size can not be less than zero') |
| 756 | |
| 757 | if self._exception is not None: |
| 758 | raise self._exception |
| 759 | |
| 760 | if n == 0: |
| 761 | return b'' |
| 762 | |
| 763 | while len(self._buffer) < n: |
| 764 | if self._eof: |
| 765 | incomplete = bytes(self._buffer) |
| 766 | self._buffer.clear() |
| 767 | raise exceptions.IncompleteReadError(incomplete, n) |
| 768 | |
| 769 | await self._wait_for_data('readexactly') |
| 770 | |
| 771 | if len(self._buffer) == n: |
| 772 | data = bytes(self._buffer) |
| 773 | self._buffer.clear() |
| 774 | else: |
| 775 | data = bytes(memoryview(self._buffer)[:n]) |
| 776 | del self._buffer[:n] |
| 777 | self._maybe_resume_transport() |
| 778 | return data |
| 779 | |
| 780 | def __aiter__(self): |
| 781 | return self |