private function flushBuffer()
    {
        $bufferLength = $this->numberOfBytes($this->buffer);
        if ($bufferLength == 0) {
            return;
        }

        $bytesWritten = $this->decoratedObject->write($this->buffer);
        if ($bytesWritten < $bufferLength) {
            throw new \RuntimeException(
                sprintf(
                    'Expected to flush buffer of %d bytes but only %d bytes were written.',
                    $bufferLength,
                    $bytesWritten
                )
            );
        }

        $this->buffer = '';
    }