def METHOD_NAME(
    self, stream: libvirt.virStream, events: Union[int, bytes], context: Any
) -> None:
    if events & libvirt.VIR_STREAM_EVENT_READABLE:
        # Data is available to be read.
        while True:
            try:
                data = stream.recv(libvirt.virStorageVol.streamBufSize)
            except libvirt.libvirtError:
                # An error occured. So, close the stream.
                self._close_stream(True)
                break
            if data == -2:
                # No more data available at the moment.
                assert self._log_file
                self._log_file.flush()
                break
            if len(data) == 0:
                # EOF reached.
                self._close_stream(False)
                break
            assert self._log_file
            self._log_file.write(data)
    if (
        events & libvirt.VIR_STREAM_EVENT_ERROR
        or events & libvirt.VIR_STREAM_EVENT_HANGUP
    ):
        # Stream is shutting down. So, close it.
        self._close_stream(True)