示例#1
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;
  }
示例#2
0
  private long downloadFile(TTask.Task task) throws Exception {
    this.mAndroidHttpClient = AndroidHttpClient.newInstance(this.TAG);
    HttpGet httpGet = new HttpGet(this.mUrl);
    HttpResponse response = this.mAndroidHttpClient.execute(httpGet);
    this.mTotalSize = response.getEntity().getContentLength();

    File file = new File(this.mFilePath, this.mFileName);

    if ((file.length() > 0L) && (this.mTotalSize > 0L) && (this.mTotalSize > file.length())) {
      httpGet.addHeader("Range", "bytes=" + file.length() + "-");
      this.mPreviousFileSize = file.length();

      this.mAndroidHttpClient.close();
      this.mAndroidHttpClient = AndroidHttpClient.newInstance("DownloadTask");
      response = this.mAndroidHttpClient.execute(httpGet);
      TLog.v(this.TAG, "File is not complete, .");
      TLog.v(
          this.TAG, "download now,File length:" + file.length() + " totalSize:" + this.mTotalSize);
    } else if ((file.exists()) && (this.mTotalSize == file.length())) {
      TLog.v(this.TAG, "Output file already exists. Skipping download.");
      return 0L;
    }

    long storage = TStorageUtils.getAvailableStorage();
    TLog.i(this.TAG, "storage:" + storage + " totalSize:" + this.mTotalSize);
    if (this.mTotalSize - file.length() > storage) {
      setErrorCode(1);
      task.stopTask();
      this.mAndroidHttpClient.close();
      return 0L;
    }
    try {
      this.mRandomAccessFile = new ProgressReportingRandomAccessFile(file, "rw");
    } catch (FileNotFoundException e) {
      TLog.v(this.TAG, "OutputStream Error");
    }

    InputStream input = null;
    try {
      input = response.getEntity().getContent();
    } catch (IOException ex) {
      setErrorCode(3);
      this.mAndroidHttpClient.close();
      TLog.v(this.TAG, "InputStream Error" + ex.getMessage());
      return 0L;
    }

    int bytesCopied = copy(input, this.mRandomAccessFile, task);

    if ((this.mPreviousFileSize + bytesCopied != this.mTotalSize)
        && (this.mTotalSize != -1L)
        && (!task.isCancel())) {
      throw new IOException("Download incomplete: " + bytesCopied + " != " + this.mTotalSize);
    }

    this.mRandomAccessFile.close();
    this.mAndroidHttpClient.close();
    this.mAndroidHttpClient = null;
    this.mRandomAccessFile = null;
    TLog.v(this.TAG, "Download completed successfully.");
    return bytesCopied;
  }