Skip to content

Commit

Permalink
Add a loading bar for downloads over HTTP
Browse files Browse the repository at this point in the history
- I am very proud of this
  • Loading branch information
aytee17 committed Dec 9, 2017
1 parent 4e10dbb commit 497e4f7
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 13 deletions.
78 changes: 67 additions & 11 deletions src/main/java/IO.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.Timer;
import java.util.TimerTask;

/**
* @author Andrew Tang
*/
public class IO {
// Pulled this variable out of writeInputToFile so the timer that displays the loading bar is guaranteed access
private static int totalRead = 0;

public static File getFileFromURL(String filePath, String urlPath) throws IOException {
private static void showFileSize(int contentLength) {
double sizeInMb = (contentLength / ((double)(1024*1024)));
double sizeInKb = (contentLength / 1024);

double size = sizeInMb > 1 ? sizeInMb : sizeInKb;
String unit = sizeInMb > 1 ? "MB" : "KB";

Main.print("File size: " + new DecimalFormat("#.##").format(size) + unit);
}

public static File getFileFromURL(String filePath, String urlPath, boolean showFileSize) throws IOException {
URL url = new URL(urlPath);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();;

Expand All @@ -18,7 +33,7 @@ public static File getFileFromURL(String filePath, String urlPath) throws IOExce
if (responseCode % 300 < 100) {
String redirect = httpConnection.getHeaderField("Location");
Main.print("Redirecting to " + redirect);
return getFileFromURL(filePath, redirect);
return getFileFromURL(filePath, redirect, showFileSize);
}
// Everything else
else if (responseCode != 200) {
Expand All @@ -27,35 +42,76 @@ else if (responseCode != 200) {
}

int contentLength = httpConnection.getContentLength();
double sizeInMb = (contentLength / ((double)(1024*1024)));
double sizeInKb = (contentLength / 1024);

double size = sizeInMb > 1 ? sizeInMb : sizeInKb;
String unit = sizeInMb > 1 ? "MB" : "KB";

Main.print("File size: " + size + unit);
if (showFileSize) {
showFileSize(contentLength);
}

InputStream inputStream = httpConnection.getInputStream();
writeInputToFile(inputStream, file);
writeInputToFile(inputStream, file, contentLength);
return file;
}

public static void writeInputToFile(InputStream inputStream, File outputFile) throws FileNotFoundException {
private static void updateLoadingBar () {

}

public static void writeInputToFile (InputStream inputStream, File outputFile) throws FileNotFoundException{
writeInputToFile(inputStream, outputFile, 0);
}

public static void writeInputToFile(InputStream inputStream, File outputFile, int contentSize)
throws FileNotFoundException {

BufferedInputStream bufferedInput = new BufferedInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(outputFile);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(fileOutputStream);

byte[] buffer = new byte[1024];
int bytesRead;

Timer timer = new Timer();
if (contentSize > 0) {
timer.schedule(new TimerTask() {
@Override
public void run() {
double fraction = totalRead / (double) contentSize;
char blockFill = '▉';
char blockEmpty = ' ';
int total_blocks = 50;
double percentageBlocks = Math.ceil(fraction*total_blocks);
double leftOver = total_blocks - percentageBlocks;

System.out.print("\r|");
for (int i = 0; i <= percentageBlocks; i++) {
System.out.print(blockFill);
}
for (int i = 0; i < leftOver; i++) {
System.out.print(blockEmpty);
}
double percentageCompleted = Math.floor(fraction*100*10)/10;
System.out.print("| " + percentageCompleted + "% complete");

}
}, 0, 100);
}

try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
bufferedOutput.write(buffer, 0, bytesRead);
totalRead += bytesRead;
}
bufferedInput.close();
bufferedOutput.close();

// Let the timer finish writing the loading bar by allowing 1 more tick
Thread.sleep(200);
timer.cancel();
totalRead = 0;
}
catch (IOException exception) {
exception.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
7 changes: 5 additions & 2 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ public static void main(String[] args) {
if (line.hasOption(URL_OPTION)) {
String gtfsURL = line.getOptionValue(URL_OPTION);
print("Downloading GTFS feed from: " + gtfsURL);
gtfsFile = IO.getFileFromURL(gtfsPath + System.getProperty("file.separator") + "GTFS.zip", gtfsURL);
print("Feed downloaded.");
gtfsFile = IO.getFileFromURL(
gtfsPath + System.getProperty("file.separator") + "GTFS.zip",
gtfsURL,
true);
print("\nFeed downloaded.");
} else {
gtfsFile = new File(gtfsPath);
}
Expand Down

0 comments on commit 497e4f7

Please sign in to comment.