edu.tsinghua.lumaqq.QQShowDownloadThread.java Source code

Java tutorial

Introduction

Here is the source code for edu.tsinghua.lumaqq.QQShowDownloadThread.java

Source

/*
* LumaQQ - Java QQ Client
*
* Copyright (C) 2004 luma <stubma@163.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package edu.tsinghua.lumaqq;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * QQ?QQShowManager
 * 
 * @author luma
 */
public class QQShowDownloadThread extends Thread {
    // Log
    private static Log log = LogFactory.getLog(QQShowDownloadThread.class);

    // QQShowManager
    private QQShowManager sm;
    // ?QQ?
    private int qqNum;
    // ?
    private BufferedInputStream bis;

    /**
     * 
     * @param qqNum ?QQ?
     */
    public QQShowDownloadThread(int qqNum) {
        this.qqNum = qqNum;
        setDaemon(true);
    }

    /* (non-Javadoc)
     * @see java.lang.Runnable#run()
     */
    @Override
    public void run() {
        sm = QQShowManager.getInstance();
        // URL
        String urlString = sm.getQQShowUrlString(qqNum);
        URL url = null;
        try {
            url = new URL(urlString);
        } catch (MalformedURLException e) {
            sm.threadCallback(qqNum);
            return;
        }
        File tempFile = null;
        FileOutputStream fos = null;
        try {
            log.debug("QQ?" + urlString);
            // 
            tempFile = new File(QQShowManager.QQ_SHOW_CACHE_DIR + qqNum + "-temp.gif");
            if (!tempFile.exists())
                tempFile.createNewFile();
            // ?
            fos = new FileOutputStream(tempFile);
            // ??
            byte[] buf = new byte[1024];
            bis = new BufferedInputStream(url.openStream());
            log.debug("??");
            for (int i = bis.read(buf, 0, 1024); i != -1; i = bis.read(buf, 0, 1024))
                fos.write(buf, 0, i);
            // ???????
            fos.close();
            fos = null;
            File file = new File(QQShowManager.QQ_SHOW_CACHE_DIR + qqNum + ".gif");
            log.debug("??" + (tempFile.renameTo(file) ? "?" : ""));
            log.debug("?");
        } catch (IOException e) {
            log.error("QQ???");
            // 
            if (fos != null) {
                try {
                    fos.close();
                    fos = null;
                } catch (IOException e1) {
                    log.error(e.getMessage());
                }
            }
            if (tempFile != null && tempFile.exists())
                tempFile.delete();
        } finally {
            try {
                if (fos != null)
                    fos.close();
                if (bis != null)
                    bis.close();
            } catch (IOException e1) {
                log.error(e1.getMessage());
            }
            sm.threadCallback(qqNum);
            log.debug("" + qqNum + " QQ");
        }
    }
}