Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company.chapter2;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.concurrent.TimeUnit;
- public class TwoFileDownloadingWithJoin {
- public static void main(String[] args) {
- Thread down1 = new FileDownloader("https://goo.gl/nqZJn4", "meet1.jpg");
- Thread down2 = new FileDownloader("https://goo.gl/UoSMMt", "meet2.jpg");
- down1.setName("file1 downloader");
- down2.setName("file2 downloader");
- try {
- down1.start();
- down2.start();
- TimeUnit.MILLISECONDS.sleep(100);
- down1.join();
- down2.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- System.out.println("Download Complete");
- }
- }
- class FileDownloader extends Thread {
- private String url;
- private String fileName;
- private double totRead;
- private ProgressUpdater progressUpdater;
- FileDownloader(String url, String fileName) {
- this.url = url;
- this.fileName = fileName;
- this.progressUpdater = new ProgressUpdater(this, fileName);
- }
- double getTotRead() {
- return totRead;
- }
- @Override
- public void run() {
- File destinationFile = new File(fileName);
- try {
- System.out.println("Starting download of " + fileName);
- URL fileUrl = new URL(url);
- HttpURLConnection connection = (HttpURLConnection) fileUrl.openConnection();
- if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
- double fileSize = Double.parseDouble(connection.getHeaderField("content-length"));
- InputStream fis = connection.getInputStream();
- FileOutputStream fos = new FileOutputStream(destinationFile);
- byte[] buffer = new byte[1024];
- double currRead;
- double totSize = fileSize / 1024.0;
- progressUpdater.setTotSize(totSize);
- progressUpdater.start();
- while ((currRead = fis.read(buffer)) != -1) {
- fos.write(buffer);
- totRead += currRead / 1024;
- }
- fis.close();
- fos.close();
- progressUpdater.kill();
- progressUpdater.join();
- } else {
- System.out.println("Error: " + connection.getResponseMessage());
- }
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- class ProgressUpdater extends Thread {
- private FileDownloader fileDownloader;
- private volatile boolean killed;
- private String fileName;
- private double totSize;
- public ProgressUpdater(FileDownloader fileDownloader, String fileName) {
- this.fileDownloader = fileDownloader;
- this.fileName = fileName;
- killed = false;
- }
- public void kill() {
- killed = true;
- }
- public void setTotSize(double totSize) {
- this.totSize = totSize;
- }
- @Override
- public void run() {
- boolean hundredPercent = false;
- while (!killed || !hundredPercent) {
- double totRead = fileDownloader.getTotRead();
- System.out.println(String.format("%s: %.2fKB of %.2fKB downloaded(%.2f%%)", fileName, totRead, totSize, 100 * totRead / totSize));
- hundredPercent = (100 * totRead / totSize) > 99.00;
- try {
- TimeUnit.MILLISECONDS.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement