Ejemplo n.º 1
0
  public void stopTask() {
    if (this.mDownloadTask != null) {
      this.mDownloadTask.stopTask();
    }
    this.mDownloadTask = null;

    if (this.mAndroidHttpClient != null) {
      this.mAndroidHttpClient.close();
    }
    try {
      if (this.mRandomAccessFile != null) {
        this.mRandomAccessFile.close();
        this.mRandomAccessFile = null;
      }
    } catch (Exception localException) {
    }
    if (this.mBuffer != null) {
      this.mBuffer = null;
    }

    if (TMDownloadManager.getInstance().getTIDownloadTaskListener() != null) {
      TMDownloadManager.getInstance().getTIDownloadTaskListener().onDownloadTaskUpdate(this);
    }
    if (TMDownloadManager.getInstance().getTIDownloadTaskListener() != null)
      TMDownloadManager.getInstance().getTIDownloadTaskListener().onDownloadTaskCancel(this);
  }
Ejemplo n.º 2
0
 public void onTask(TTask.Task task, TTask.TaskEvent event, Object[] params) {
   if ((this.mDownloadTask != null)
       && (this.mDownloadTask.getTask() == task)
       && (event != TTask.TaskEvent.Before))
     if (event == TTask.TaskEvent.Cancel) {
       TMDownloadManager.getInstance().delDownloadTask(this);
     } else if ((event != TTask.TaskEvent.Update) && (event == TTask.TaskEvent.Work)) {
       try {
         downloadFile(task);
       } catch (Exception e) {
         this.mException = e;
         setErrorCode(3);
       }
       if (this.mAndroidHttpClient != null) {
         this.mAndroidHttpClient.close();
       }
       try {
         if (this.mRandomAccessFile != null) {
           this.mRandomAccessFile.close();
           this.mRandomAccessFile = null;
         }
       } catch (Exception localException1) {
       }
     }
 }
Ejemplo n.º 3
0
  public int copy(InputStream input, RandomAccessFile out, TTask.Task task)
      throws Exception, IOException {
    this.mBuffer = new byte[8192];
    BufferedInputStream in = new BufferedInputStream(input, 8192);
    TLog.v(this.TAG, "length" + out.length());
    out.seek(out.length());

    int count = 0;
    int byteCount = 0;
    long errorBlockTimePreviousTime = -1L;
    long expireTime = 0L;
    try {
      while (!task.isCancel()) {
        byteCount = in.read(this.mBuffer, 0, 8192);
        if (byteCount == -1) {
          break;
        }
        out.write(this.mBuffer, 0, byteCount);
        count += byteCount;

        if (!TMDownloadManager.getInstance().isOnline()) {
          task.stopTask();
          setErrorCode(2);
          break;
        }

        if (this.mSpeed == 0L) {
          if (errorBlockTimePreviousTime > 0L) {
            expireTime = System.currentTimeMillis() - errorBlockTimePreviousTime;
            if (expireTime > 30000L) {
              setErrorCode(2);
              task.stopTask();
            }
          } else {
            errorBlockTimePreviousTime = System.currentTimeMillis();
          }
        } else {
          expireTime = 0L;
          errorBlockTimePreviousTime = -1L;
        }
      }
    } finally {
      try {
        out.close();
      } catch (IOException e) {
        setErrorCode(3);
        TLog.e(this.TAG, e.getMessage());
      }
      try {
        in.close();
      } catch (IOException e) {
        setErrorCode(3);
        TLog.e(this.TAG, e.getMessage());
      }
    }

    this.mBuffer = null;
    return count;
  }