public function write($string) {
        if (!$this->isWritable()) {
            throw new \RuntimeException('Stream is not writable');
        }
        if (!$this->eof()) {
            // copy the data after the cursor to another stream so that it can be written back
            $tempStream = $this->createStream();
            stream_copy_to_stream($this->stream, $tempStream);
            // reset cursors to their positions prior to the reading
            fseek($tempStream, 0);
            $this->seek($this->cursor);
        }
        $retval = fwrite($this->stream, $string);
        if ($retval === false) {
            throw new \RuntimeException('Write failed (fwrite returned false)');
        }
        $this->flen += $retval;
        $this->cursor += $retval;
        if (isset($tempStream)) {
            stream_copy_to_stream($tempStream, $this->stream);
            fclose($tempStream);
            $this->seek($this->cursor);
        }
        return $retval;
    }