Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. private void downloadAndEncrypt() throws Exception {
  2.  
  3. URL url = new URL(mUrl);
  4. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  5.  
  6. long downloaded = 0;
  7.  
  8. if(mFile.exists()){
  9. Logger.e("File exists. Resume Download");
  10. downloaded = mFile.length();
  11. connection.setRequestProperty("Range", "bytes=" + (int)downloaded + "-");
  12. }else{
  13. Logger.e("File doesn't exists. Start Download");
  14. }
  15. connection.connect();
  16.  
  17. int lenghtOfFile = connection.getContentLength();
  18.  
  19. if (connection.getResponseCode() != HttpURLConnection.HTTP_OK && connection.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) {
  20. throw new IOException("server error: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
  21. }
  22.  
  23. BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
  24. FileOutputStream fileOutputStream;
  25. if (downloaded == 0)
  26. fileOutputStream = new FileOutputStream(mFile);
  27. else
  28. fileOutputStream = new FileOutputStream(mFile, true);
  29.  
  30. CipherOutputStream cipherOutputStream = new CipherOutputStream(fileOutputStream, mCipher);
  31.  
  32. byte buffer[] = new byte[1024 * 1024];
  33. int bytesRead;
  34. long total = 0;
  35. while ((bytesRead = inputStream.read(buffer)) != -1) {
  36. total += bytesRead;
  37. publishProgress((int) ((total * 100) / lenghtOfFile));
  38. cipherOutputStream.write(buffer, 0, bytesRead);
  39. }
  40.  
  41. inputStream.close();
  42. cipherOutputStream.close();
  43. connection.disconnect();
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement