<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write(self, data, timeout):
 """Write data to this stream, using the given timeouts.PolledTimeout."""

    if not self.remote_id:
      raise usb_exceptions.AdbStreamClosedError(
          'Cannot write() to half-opened %s', self)
    if self.closed_state != self.ClosedState.OPEN:
      raise usb_exceptions.AdbStreamClosedError(
          'Cannot write() to closed %s', self)
    elif self._expecting_okay:
      raise usb_exceptions.AdbProtocolError(
          'Previous WRTE failed, %s in unknown state', self)

    # Make sure we only have one WRTE in flight at a time, because ADB doesn't
    # identify which WRTE it is ACK'ing when it sends the OKAY message back.
    with self._write_lock:
      self._expecting_okay = True
      self._send_command('WRTE', timeout, data)
      self._read_messages_until_true(lambda: not self._expecting_okay, timeout)