public function write($filename = null)
    {
        if ($filename === null) {
            $filename = $this->getFilename();
        }
        if (empty($filename)) {
            $filename = 'file';
        }

        if (! $handle = fopen($filename, 'w')) {
            trigger_error('Can not open the destination file', E_USER_ERROR);
            return 0;
        }

        $written = $this->copyToResource($handle);
        fclose($handle);

        return $written;
    }