/*!
Writes the content of the string \a $data to the file.
If optional \c $length parameter is supplied writing will stop after
length is reached or the end of the string is reached, whichever comes first.
\return the number of bytes that was written.
970565
function write( $data, $length = false )
    {
        if ( !$this->isOpen() )
        {
            eZDebug::writeError( "A file is not currently opened, cannot write.", __METHOD__ );
            return false;
        }
        if ( $length < 0 )
        {
            eZDebug::writeError( "length cannot be negative ($length)", __METHOD__ );
            return false;
        }
        return $this->doWrite( $data, $length );
    }
274841
public function stream_write(string $data): int
    {
        if (self::READONLY === $this->mode) {
            return 0;
        }

        if ($this->content->isWritable(vfsStream::getCurrentUser(), vfsStream::getCurrentGroup()) === false) {
            return 0;
        }

        if (self::$quota->isLimited()) {
            $data = substr($data, 0, self::$quota->spaceLeft(self::$root->sizeSummarized()));
        }

        return $this->content->write($data);
    }
1345053
def write(self, data):
        '''  '''
        if data == '\n':  # print does this
            return self.stream.write(data)
        else:
            bytes_ = 0
            for line in data.splitlines(True):
                nl = ''
                if line.endswith('\n'):  # mv nl to end:
                    line = line[:-1]
                    nl = '\n'
                bytes_ += self.stream.write(
                                f'{self.start}{line}{self.default}{nl}'
                          ) or 0  # in case None returned (on Windows)
            return bytes_
1274890
protected function _fwrite($data, $length=null)
    {
        if ($length === null ) {
            fwrite($this->_fileHandle, $data);
        } else {
            fwrite($this->_fileHandle, $data, $length);
        }
    }
1028585
public function onWriteReady()
    {
        $bytesWritten = fwrite($this->socket, $this->outputBuffer);

        /*
         * Fragment below looks weird at first, but it's perfectly logical.
         * Most of the time buffer is completely written by fwrite() into system TCP buffer (which is 8KB by default). In
         * that case using (rather expansive) substr() call can be avoided by checking whatever buffer was completely
         * written. To do so efficiently isset() and direct character access is used. fwrite() returns NUMBER OF BYTES
         * written. Character in strings are indexed from 0 (like arrays), so calling fwrite($socket, "abcde") return
         * 5 if it was fully written, calling isset($x[5]) produce "false" because last character have index of 4.
         */
        $this->outputBuffer = (!isset($this->outputBuffer[$bytesWritten])) ? '' : substr($this->outputBuffer,
            $bytesWritten);

        //$this->logger->debug("Sent $bytesWritten to client $this");
        //$this->logger->debug("Client $this contains " . strlen($this->outputBuffer) . ' bytes in write buffer'); //Time consuming

        return ($this->outputBuffer === '');
    }
1335220
public function write($data)
    {
        if (!$this->connected) {
            if (!$this->connect()) {
                return false;
            }
        }
        return fwrite($this->connection, $data, strlen($data));
    }
1002046
public function writeString($str) {

            $strref = strlen($str) << 1 | 0x01;
            $this->writeInt($strref);
            $this->stream->writeBuffer($str);

        }
310392
public function write($string)
    {
        $this->content = substr_replace($this->content, $string, $this->position, 0);
        $len = strlen($string);
        $this->position += $len;
        return $len;
    }
613781
public function write($handle, string $data, $length = null) : int
    {
        $bytesWritten = is_int($length) ? fwrite($handle, $data, $length) : fwrite($handle, $data);

        if (false === $bytesWritten) {
            throw new FileException('Cannot write to a file.');
        }

        return $bytesWritten;
    }
263230
static function write($file, $data)
	{
		$content = "";
		foreach($data as $key => $value)
		{
			if(is_array($value))
			{
				for($i=0; $i<count($value); $i++)
					$content .= $key."[] = \"".addslashes($value[$i])."\"\n";
			}
			elseif($value)
				$content .= $key." = \"".addslashes($value)."\"\n";
			else
				$content .= $key." = \n";
		}

		bors_function_include('fs/file_put_contents_lock');
		file_put_contents_lock($file, $content);
	}
916765
final protected function fwrite($string, $length = null)
	{
		return is_int($length)
			? fwrite($this->fhandle, $string, $length)
			: fwrite($this->fhandle, $string);
	}
710971
public function writeAttempt()
    {
        $result = self::OK;

        if (empty($this->filepath)) {
            return self::ERROR;
        }

        /**
         * Open the file for writing only. If the file does not exist, it is created.
         * If it exists, it is not truncated (as opposed to 'w'). File pointer is moved to beginning.
         *
         * "c" is needed instead of "w", because if lock is not acquired, old version of the file is returned.
         * If "w" option is used, file is truncated no matter what, and empty file is returned.
         */
        $fp = fopen($this->filepath, 'c');

        if ($fp === false) {
            return self::ERROR_OPEN;
        }

        /**
         * File locking disabled?
         */
        if (empty($this->file_lock)) {
            ftruncate($fp, 0);
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }
            fclose($fp);

            return $result;
        }

        /**
         * File locking is enabled
         *
         * Try to acquire File Write Lock. If lock is not acquired read file and return old contents.
         *
         * Recommended is: LOCK_EX | LOCK_NB.
         * LOCK_EX to acquire an exclusive lock (writer).
         * LOCK_NB - prevents flock() from blocking while locking, so that others could still read
         * (but not write) while lock is active
         */
        if (flock($fp, $this->file_lock)) {
            /**
             * since "c" was used with fopen, file is not truncated. Truncate manually.
             */
            ftruncate($fp, 0);

            //write content
            if (fwrite($fp, $this->content) === false) {
                $result = self::ERROR_WRITE;
            }

            //release lock
            flock($fp, LOCK_UN);
        } else {
            /**
             * Lock was not granted.
             */

            $result = self::ERROR_LOCK;
        }

        fclose($fp);

        return $result;
    }
558098
public function write($data, $offset)
    {
        $segno = floor($offset / $this->segsize);
        if (!isset($this->segments[$segno])) {
            if (!$this->open($segno, true)) {
                return false;
            }
        }
        $sOffset = $offset % $this->segsize;
        $d = $this->segsize - ($sOffset + mb_orig_strlen($data));
        if ($d < 0) {
            $this->write(mb_orig_substr($data, $d), ($segno + 1) * $this->segsize);
            $data = mb_orig_substr($data, 0, $d);
        }
        //Daemon::log('writing to #'.$offset.' (segno '.$segno.')');
        shmop_write($this->segments[$segno], $data, $sOffset);
        return true;
    }
130340
def write(str)
      s = str.to_s
      @length += Rack::Utils.bytesize(s)
      @writer.call s

      header["Content-Length"] = @length.to_s
      str
    end
928884
public function fwrite(string $data): int
    {
        $len = dechex(strlen($data));
        $written = fwrite($this->peer, "${len}\r\n${data}\r\n");
        if (!$written) {
            $msg = 'Cannot write to socket';
            throw new Exception\RuntimeException($msg);
        }

        return $written;
    }
242845
@Override
	public void writeBytes(String s)
    {
    	int len = s.length();
    	ensureCapacity(size+len);
    	for (int i = 0 ; i < len ; i++)
    	    buf[pos++] = (byte)s.charAt(i);
    }
    
	/* (non-Javadoc)
	 * @see java.io.DataOutput#writeChars(java.lang.String)
	 */
	@Override
	public void writeChars(String s)
	{
		int len = s.length();
		ensureCapacity(size+len*2);
        for (int i = 0 ; i < len ; i++) {
            int v = s.charAt(i);
            buf[pos++] = (byte)((v >>> 8) & 0xFF); 
            buf[pos++] = (byte)((v >>> 0) & 0xFF); 
        }
	}
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#write(byte[], int, int)
     */
    @Override
	public void write(byte[] data,int offset,int len)
    {
        int newCount = size+len; 
        ensureCapacity(newCount);
        System.arraycopy(data, offset, buf, size, len);
        size = newCount;
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeByte(int)
     */
    @Override
	public void writeByte(int b)
    {
        ensureCapacity(size+1);
        buf[size++] = (byte)b;
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeBoolean(boolean)
     */
    @Override
	public void writeBoolean(boolean v)
    {
        write(v ? 1 : 0);
    }
    
    /**
     * Read a byte value at an arbitrary position in the buffer
     * @param position the target position
     */
    public byte readByte(int position)
    {
    	return buf[position];
    }
    
    /**
     * Write a byte value at an arbitrary position in the buffer
     * @param v the byte value
     * @param position the target position
     */
    public void writeByte(byte v,int position)
    {
    	buf[position] = v;
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeShort(int)
     */
    @Override
	public void writeShort(int v)
    {
        ensureCapacity(size + 2);
        buf[size++] = (byte)((v >>>  8) & 0xFF);
        buf[size++] = (byte)((v >>>  0) & 0xFF);
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeInt(int)
     */
    @Override
	public void writeInt(int v)
    {
        ensureCapacity(size + 4);
        buf[size++] = (byte)((v >>> 24) & 0xFF);
        buf[size++] = (byte)((v >>> 16) & 0xFF);
        buf[size++] = (byte)((v >>>  8) & 0xFF);
        buf[size++] = (byte)((v >>>  0) & 0xFF);
    }
    
    /**
     * Write a int value at an arbitrary position in the stream
     * @param v the int value
     * @param pos the target position
     */
    public void writeInt(int v,int pos)
    {
        buf[pos++] = (byte)((v >>> 24) & 0xFF);
        buf[pos++] = (byte)((v >>> 16) & 0xFF);
        buf[pos++] = (byte)((v >>>  8) & 0xFF);
        buf[pos]   = (byte)((v >>>  0) & 0xFF);
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeLong(long)
     */
    @Override
	public void writeLong(long v)
    {
        ensureCapacity(size + 8);
        buf[size++] = (byte)(v >>> 56);
        buf[size++] = (byte)(v >>> 48);
        buf[size++] = (byte)(v >>> 40);
        buf[size++] = (byte)(v >>> 32);
        buf[size++] = (byte)(v >>> 24);
        buf[size++] = (byte)(v >>> 16);
        buf[size++] = (byte)(v >>>  8);
        buf[size++] = (byte)(v >>>  0);
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeFloat(float)
     */
    @Override
	public void writeFloat(float v)
    {
        writeInt(Float.floatToIntBits(v));
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeDouble(double)
     */
    @Override
	public void writeDouble(double v)
    {
        writeLong(Double.doubleToLongBits(v));
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeChar(int)
     */
    @Override
	public void writeChar(int v)
    {
        ensureCapacity(size + 2);
        buf[size++] = (byte)((v >>>  8) & 0xFF);
        buf[size++] = (byte)((v >>>  0) & 0xFF);
    }
    
    /*
     * (non-Javadoc)
     * @see java.io.DataOutput#writeUTF(java.lang.String)
     */
    @Override
	public void writeUTF(String str)
    {
        int strlen = str.length();
        if (strlen < 255)
            writeUTF(str,UTF_TYPE_1);
        else
        if (strlen < 65535)
            writeUTF(str,UTF_TYPE_2);
        else
            writeUTF(str,UTF_TYPE_4);
    }
    
    private void writeUTF( String str , byte type )
    {       
        // We need at least type + strlen bytes (1 byte/char) + 1
        int strlen = str.length();
        int rawlen = size+type+strlen+1;
        ensureCapacity(rawlen);
        
        // Write UTF type prefix
        buf[size++] = type;
        
        // Write the string length
        switch (type)
        {
            case UTF_TYPE_1 :
                buf[size++] = (byte)((strlen >>>  0) & 0xFF);
                break;
            case UTF_TYPE_2 :
                buf[size++] = (byte)((strlen >>>  8) & 0xFF);
                buf[size++] = (byte)((strlen >>>  0) & 0xFF);
                break;
            case UTF_TYPE_4 :
                buf[size++] = (byte)((strlen >>> 24) & 0xFF);
                buf[size++] = (byte)((strlen >>> 16) & 0xFF);
                buf[size++] = (byte)((strlen >>>  8) & 0xFF);
                buf[size++] = (byte)((strlen >>>  0) & 0xFF);
                break;
            default:
            	throw new IllegalArgumentException("Invalid UTF type : "+type);   
        }

        // Write the string
        int c,i = 0;
        for (i = 0 ; i < strlen ; i++)
        {
            c = str.charAt(i);
            if (!((c >= 0x0001) && (c <= 0x007F)))
                break;
            buf[size++] = (byte)c;
        }
        // Are we done ?
        if (i == strlen)
            return;

        // Fallback to full encoding mode
        for ( ; i < strlen ; i++)
        {
            c = str.charAt(i);

            if ((c >= 0x0001) && (c <= 0x007F))
            {
                // One byte
                buf[size++] = (byte)c;
            }
            else if (c > 0x07FF)
            {
                rawlen += 3;
                ensureCapacity(rawlen);
                
                buf[size++] = (byte)(0xE0 | ((c >> 12) & 0x0F));
                buf[size++] = (byte)(0x80 | ((c >> 6) & 0x3F));
                buf[size++] = (byte)(0x80 | ((c >> 0) & 0x3F));
            }
            else
            {
                rawlen += 2;
                ensureCapacity(rawlen);
                
                buf[size++] = (byte)(0xC0 | ((c >> 6) & 0x1F));
                buf[size++] = (byte)(0x80 | ((c >> 0) & 0x3F));
            }
        }
    }
1006081
public function write($data)
    {
        $content = $this->file->data();
        $content = substr($content, 0, $this->position());
        $content .= $data;
        $this->file->setData($content);
        $written = strlen($data);
        $this->offsetPosition($written);
        $this->file->setModificationTime(time());
        $this->file->setChangeTime(time());

        return $written;
    }