Pchp.Library.Streams.PhpStream.FlushWriteBuffer C# (CSharp) Method

FlushWriteBuffer() protected method

Writes all the output buffer to the underlying stream.
protected FlushWriteBuffer ( ) : bool
return bool
        protected bool FlushWriteBuffer()
        {
            // Stream may not have been used for output yet.
            if ((writeBufferSize == 0) || (writeBuffer == null)) return true;

            int flushPosition = 0;
            while (flushPosition < writePosition)
            {
                // Send as much data as possible to the underlying stream.
                int written = RawWrite(writeBuffer, flushPosition, writePosition - flushPosition);

                if (written <= 0)
                {
                    // An error occured. Clear flushed data and return.
                    if (flushPosition > 0)
                    {
                        byte[] buf = new byte[writeBufferSize];
                        Array.Copy(writeBuffer, flushPosition, buf, 0, writePosition - flushPosition);
                        writeBuffer = buf;
                    }

                    PhpException.Throw(PhpError.Warning, ErrResources.stream_write_failed, flushPosition.ToString(), writePosition.ToString());

                    return false;
                }
                else
                {
                    // Move for the next chunk.
                    flushPosition += written;
                    writeOffset += written;
                }
            }

            // All the data has been successfully flushed.
            writePosition = 0;
            return true;
        }