예제 #1
0
  @Override
  public void onCheckRequestHeaders(String requestUrl, HttpRequestBase request) {
    if (request == null) {
      throw new IllegalArgumentException("Http Request is null");
    }

    if (SUPPORT_RANGED) {
      // 目前只有大文件下载才会做此接口回调,在此回调中可以增加断点续传
      if (request instanceof HttpGet) {
        String saveFile =
            mDownloadFilenameCreateListener != null
                ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl)
                : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(
                    requestUrl);
        File bigCacheFile = new File(INPUT_STREAM_CACHE_PATH);
        if (!bigCacheFile.exists() || !bigCacheFile.isDirectory()) {
          bigCacheFile.delete();
          bigCacheFile.mkdirs();
        }

        File tempFile = new File(INPUT_STREAM_CACHE_PATH + saveFile);
        long fileSize = 0;
        if (tempFile.exists()) {
          fileSize = tempFile.length();
        } else {
          fileSize = 0;
        }

        request.addHeader("RANGE", "bytes=" + fileSize + "-");
      }
    }
  }
예제 #2
0
  // 检查缓存目录中是否已经下载过文件
  private String checkFromCache(DownloadRequest request) {
    if (request != null && !TextUtils.isEmpty(request.mDownloadUrl)) {
      String saveUrl =
          mDownloadFilenameCreateListener != null
              ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(
                  request.mDownloadUrl)
              : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(
                  request.mDownloadUrl);
      if (TextUtils.isEmpty(DOWNLOADED_FILE_DIR)) {
        return null;
      }
      File dir = new File(DOWNLOADED_FILE_DIR);
      if (!dir.exists() || dir.isFile()) {
        return null;
      }
      String extension =
          TextUtils.isEmpty(request.mFileExtension) ? "" : "." + request.mFileExtension;
      File cachedFile = new File(DOWNLOADED_FILE_DIR + saveUrl + extension);
      if (cachedFile.exists()) {

        if (DEBUG) {
          CommonUtilsConfig.LOGD_WITH_TIME(
              "<<<<< [[find in cache]] >>>>> ::::::::: " + cachedFile.getAbsolutePath());
        }
        return cachedFile.getAbsolutePath();
      }
    }
    if (DEBUG) {
      CommonUtilsConfig.LOGD_WITH_TIME(
          "<<<<< [[can not find in cache]] >>>>> ::::::::: " + request.toString());
    }
    return null;
  }
예제 #3
0
  @Override
  public String onInputStreamReturn(String requestUrl, InputStream is) {
    // if (!UtilsRuntime.isSDCardReady()) {
    // UtilsConfig.LOGD("return because unmount the sdcard");
    // return null;
    // }

    if (DEBUG) {
      CommonUtilsConfig.LOGD("");
      CommonUtilsConfig.LOGD("//-------------------------------------------------");
      CommonUtilsConfig.LOGD("||");
      CommonUtilsConfig.LOGD("|| [[FileDownloader::onInputStreamReturn]] : ");
      CommonUtilsConfig.LOGD("||      try to download [[BIG]] file with url : " + requestUrl);
      CommonUtilsConfig.LOGD("||");
      CommonUtilsConfig.LOGD("\\-------------------------------------------------");
      CommonUtilsConfig.LOGD("");
    }

    if (is != null) {
      String saveUrl =
          mDownloadFilenameCreateListener != null
              ? mDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl)
              : mDefaultDownloadFilenameCreateListener.onFilenameCreateWithDownloadUrl(requestUrl);
      File bigCacheFile = new File(INPUT_STREAM_CACHE_PATH);
      if (!bigCacheFile.exists() || !bigCacheFile.isDirectory()) {
        bigCacheFile.delete();
        bigCacheFile.mkdirs();
      }

      long curTime = 0;
      if (DEBUG) {
        CommonUtilsConfig.LOGD(
            "try to download from inputstream to local path = "
                + INPUT_STREAM_CACHE_PATH
                + saveUrl
                + " for orgin URL : "
                + requestUrl);
        curTime = System.currentTimeMillis();
      }

      // download file
      int totalSize = 0;
      try {
        totalSize = is.available();
      } catch (Exception e) {
        e.printStackTrace();
      }

      long downloadSize = 0;
      String savePath = null;
      String targetPath = INPUT_STREAM_CACHE_PATH + saveUrl;
      byte[] buffer = new byte[4096 * 2];
      File f = new File(targetPath);
      int len;
      OutputStream os = null;
      boolean isClosed = false;
      try {
        if (f.exists()) {
          downloadSize = f.length();
        }

        os = new FileOutputStream(f, true);
        while ((len = is.read(buffer)) != -1) {
          os.write(buffer, 0, len);

          // add listener to Notify UI
          downloadSize += len;
          handleProcess(requestUrl, totalSize, (int) downloadSize);

          if (RUNTIME_CLOSE_SUPPORTED) {
            DownloadRequest r = findCacelRequest(requestUrl);
            if (r != null && r.mStatus == DownloadRequest.STATUS_CANCEL) {
              CommonUtilsConfig.LOGD("try to close is >>>>>>>>>>>>>>>>>>>>");
              is.close();
              isClosed = true;
            }
          }
        }
        savePath = targetPath;
      } catch (Exception ex) {
        ex.printStackTrace();
      } finally {
        if (os != null) {
          try {
            os.close();
          } catch (Exception e) {
            e.printStackTrace();
          }
        }

        buffer = null;
      }
      // end download

      try {
        if (!isClosed) {
          is.close();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }

      if (!isClosed && !TextUtils.isEmpty(savePath) && checkInputStreamDownloadFile(savePath)) {
        if (DEBUG) {
          long successTime = System.currentTimeMillis();
          CommonUtilsConfig.LOGD(
              "[[onInputStreamReturn]] save Request url : "
                  + saveUrl
                  + " success ||||||| and the saved file size : "
                  + FileUtil.convertStorage(new File(savePath).length())
                  + ", save cost time = "
                  + (successTime - curTime)
                  + "ms");
        }

        return savePath;
      } else {
        // 遗留文件,用于下次的断点下载
        if (DEBUG) {
          CommonUtilsConfig.LOGD(
              "===== failed to downlaod requestUrl : "
                  + requestUrl
                  + " beacuse the debug 断点 =====");
        }
        return null;
      }
    } else {
      if (DEBUG) {
        CommonUtilsConfig.LOGD(
            "===== failed to downlaod requestUrl : "
                + requestUrl
                + " beacuse requestUrl is NULL =====");
      }
    }

    return null;
  }