public function length($utf8 = false)
    {
        $pos = $this->pos();

        if ($utf8 && $this->_utf8_char) {
            $this->rewind();
            $len = 0;
            while ($this->getChar() !== false) {
                ++$len;
            }
        } elseif (!$this->end()) {
            throw new Horde_Stream_Exception('ERROR');
        } else {
            $len = $this->pos();
        }

        if (!$this->seek($pos, false)) {
            throw new Horde_Stream_Exception('ERROR');
        }

        return $len;
    }