public function writeLiteral($data, $length, $binary = false)
    {
        $this->_buffer = '';
        $success = false;

        if ($data instanceof Horde_Stream) {
            $data = $data->stream;
        }

        if (rewind($data)) {
            $success = true;
            while (!feof($data)) {
                if ((($read_data = fread($data, 8192)) === false) ||
                    (fwrite($this->_stream, $read_data) === false)) {
                    $success = false;
                    break;
                }
            }
        }

        if (!$success) {
            $this->client_debug = true;
            throw new Horde_Imap_Client_Exception(
                Horde_Imap_Client_Translation::r("Server write error."),
                Horde_Imap_Client_Exception::SERVER_WRITEERROR
            );
        }

        if ($this->client_debug && !empty($this->_params['debugliteral'])) {
            rewind($data);
            while (!feof($data)) {
                $this->_params['debug']->raw(fread($data, 8192));
            }
        } else {
            $this->_params['debug']->client('[' . ($binary ? 'BINARY' : 'LITERAL') . ' DATA: ' . $length . ' bytes]');
        }
    }