예제 #1
0
 @Override
 public void run() {
   while (!isStop() && !isFinish()) {
     try {
       URL url = new URL(info.getUrl());
       HttpURLConnection con = (HttpURLConnection) url.openConnection();
       String prop = "bytes=" + info.getPos() + "-";
       System.out.println(prop);
       System.out.println(info.getFileSize());
       con.setRequestProperty("RANGE", prop);
       InputStream is = con.getInputStream();
       raf = new RandomAccessFile(info.getFileName(), "rw");
       raf.seek(info.getPos());
       byte[] buffer = new byte[BUFFER_SIZE];
       int b = -1;
       while ((b = is.read(buffer)) != -1 && !isStop()) {
         raf.write(buffer, 0, b);
         info.setPos(info.getPos() + b);
       }
       if (!isStop()) {
         isFinish = true;
       }
     } catch (MalformedURLException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     } finally {
       try {
         raf.close();
       } catch (IOException e) {
       }
     }
   }
 }
예제 #2
0
 /**
  * Create a builder from the DownloadInfo object.
  *
  * @param downloadInfo DownloadInfo object from which builder fields are populated.
  * @return A builder initialized with fields from downloadInfo object.
  */
 public static Builder fromDownloadInfo(final DownloadInfo downloadInfo) {
   Builder builder = new Builder();
   builder
       .setUrl(downloadInfo.getUrl())
       .setUserAgent(downloadInfo.getUserAgent())
       .setMimeType(downloadInfo.getMimeType())
       .setCookie(downloadInfo.getCookie())
       .setFileName(downloadInfo.getFileName())
       .setDescription(downloadInfo.getDescription())
       .setFilePath(downloadInfo.getFilePath())
       .setReferer(downloadInfo.getReferer())
       .setOriginalUrl(downloadInfo.getOriginalUrl())
       .setContentLength(downloadInfo.getContentLength())
       .setDownloadGuid(downloadInfo.getDownloadGuid())
       .setHasUserGesture(downloadInfo.hasUserGesture())
       .setContentDisposition(downloadInfo.getContentDisposition())
       .setIsGETRequest(downloadInfo.isGETRequest())
       .setPercentCompleted(downloadInfo.getPercentCompleted())
       .setTimeRemainingInMillis(downloadInfo.getTimeRemainingInMillis())
       .setIsResumable(downloadInfo.isResumable())
       .setIsPaused(downloadInfo.isPaused())
       .setIsOffTheRecord(downloadInfo.isOffTheRecord())
       .setIsOfflinePage(downloadInfo.isOfflinePage());
   return builder;
 }