Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private void downloadAndEncrypt() throws Exception {
- URL url = new URL(mUrl);
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- long downloaded = 0;
- if(mFile.exists()){
- Logger.e("File exists. Resume Download");
- downloaded = mFile.length();
- connection.setRequestProperty("Range", "bytes=" + (int)downloaded + "-");
- }else{
- Logger.e("File doesn't exists. Start Download");
- }
- connection.connect();
- int lenghtOfFile = connection.getContentLength();
- if (connection.getResponseCode() != HttpURLConnection.HTTP_OK && connection.getResponseCode() != HttpURLConnection.HTTP_PARTIAL) {
- throw new IOException("server error: " + connection.getResponseCode() + ", " + connection.getResponseMessage());
- }
- BufferedInputStream inputStream = new BufferedInputStream(connection.getInputStream());
- FileOutputStream fileOutputStream;
- if (downloaded == 0)
- fileOutputStream = new FileOutputStream(mFile);
- else
- fileOutputStream = new FileOutputStream(mFile, true);
- CipherOutputStream cipherOutputStream = new CipherOutputStream(fileOutputStream, mCipher);
- byte buffer[] = new byte[1024 * 1024];
- int bytesRead;
- long total = 0;
- while ((bytesRead = inputStream.read(buffer)) != -1) {
- total += bytesRead;
- publishProgress((int) ((total * 100) / lenghtOfFile));
- cipherOutputStream.write(buffer, 0, bytesRead);
- }
- inputStream.close();
- cipherOutputStream.close();
- connection.disconnect();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement