def _handle_read_event(self):
        """
        Handle a read event raised on the channel.
        """
        if self.ssl_enabled and not self._ssl_handshake_done:
            self._ssl_do_handshake()
            return

        while True:
            try:
                data = self._socket_recv()
            except socket.error as err:
                self._safely_call(self.on_read_error, err)
                return

            if not data:
                break
            else:
                self._recv_buffer += data

                if len(self._recv_buffer) > self._recv_buffer_size_limit:
                    # Try processing the buffer to reduce its length.
                    self._process_recv_buffer()

                    # If the buffer's still too long, overflow error.
                    if len(self._recv_buffer) > self._recv_buffer_size_limit:
                        e = StreamBufferOverflow("Buffer length exceeded upper limit on %r." % self)
                        self._safely_call(self.on_overflow_error, e)
                        return

        self._process_recv_buffer()

        # This block was moved out of the above loop to address issue #41.
        if data is None:
            self.close(flush=False)
dataset/ETHPy150Open ecdavis/pants/pants/stream.py/Stream._handle_read_event/original