Skip to content

feat: add progress bar to file Downloader #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: ors_4.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions core/src/main/java/com/graphhopper/util/Downloader.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@
*/
package com.graphhopper.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.util.function.LongConsumer;
import java.util.zip.GZIPInputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
Expand All @@ -28,6 +33,8 @@
* @author Peter Karich
*/
public class Downloader {
private static final Logger logger = LoggerFactory.getLogger(Downloader.class);
private static final int BUFFER_SIZE = 8 * 1024;
private final String userAgent;
private String referrer = "http://graphhopper.com";
private String acceptEncoding = "gzip, deflate";
Expand All @@ -39,12 +46,7 @@ public Downloader(String userAgent) {

public static void main(String[] args) throws IOException {
new Downloader("GraphHopper Downloader").downloadAndUnzip("http://graphhopper.com/public/maps/0.1/europe_germany_berlin.ghz", "somefolder",
new ProgressListener() {
@Override
public void update(long val) {
System.out.println("progress:" + val);
}
});
val -> System.out.println("progress:" + val));
}

public Downloader setTimeout(int timeout) {
Expand Down Expand Up @@ -80,17 +82,17 @@ public InputStream fetch(HttpURLConnection connection, boolean readErrorStreamNo
try {
String encoding = connection.getContentEncoding();
if (encoding != null && encoding.equalsIgnoreCase("gzip"))
is = new GZIPInputStream(is);
is = new GZIPInputStream(is, BUFFER_SIZE);
else if (encoding != null && encoding.equalsIgnoreCase("deflate"))
is = new InflaterInputStream(is, new Inflater(true));
is = new InflaterInputStream(is, new Inflater(true), BUFFER_SIZE);
} catch (IOException ex) {
}

return is;
}

public InputStream fetch(String url) throws IOException {
return fetch((HttpURLConnection) createConnection(url), false);
return fetch(createConnection(url), false);
}

public HttpURLConnection createConnection(String urlStr) throws IOException {
Expand All @@ -112,36 +114,28 @@ public HttpURLConnection createConnection(String urlStr) throws IOException {
public void downloadFile(String url, String toFile) throws IOException {
HttpURLConnection conn = createConnection(url);
InputStream iStream = fetch(conn, false);
int size = 8 * 1024;
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), size);
InputStream in = new BufferedInputStream(iStream, size);
try {
byte[] buffer = new byte[size];
int numRead;
while ((numRead = in.read(buffer)) != -1) {
writer.write(buffer, 0, numRead);
}
BufferedOutputStream writer = new BufferedOutputStream(new FileOutputStream(toFile), BUFFER_SIZE);
InputStream in = new BufferedInputStream(iStream, BUFFER_SIZE);
String file = Path.of(toFile).getFileName().toString();
logger.info("Downloading {} [{}]", url, conn.getContentLengthLong());
try (OutputStream progress = new ProgressOutputStream(writer, "downloading %s ..".formatted(file), conn.getContentLengthLong(), logger::info)) {
in.transferTo(progress);
} finally {
Helper.close(iStream);
Helper.close(writer);
Helper.close(in);
}
}

public void downloadAndUnzip(String url, String toFolder, final ProgressListener progressListener) throws IOException {
public void downloadAndUnzip(String url, String toFolder, final LongConsumer progressListener) throws IOException {
HttpURLConnection conn = createConnection(url);
final int length = conn.getContentLength();
InputStream iStream = fetch(conn, false);

new Unzipper().unzip(iStream, new File(toFolder), new ProgressListener() {
@Override
public void update(long sumBytes) {
progressListener.update((int) (100 * sumBytes / length));
}
});
new Unzipper().unzip(iStream, new File(toFolder), sumBytes -> progressListener.accept((int) (100 * sumBytes / length)));
}

public String downloadAsString(String url, boolean readErrorStreamNoException) throws IOException {
return Helper.isToString(fetch((HttpURLConnection) createConnection(url), readErrorStreamNoException));
return Helper.isToString(fetch(createConnection(url), readErrorStreamNoException));
}
}
85 changes: 85 additions & 0 deletions core/src/main/java/com/graphhopper/util/ProgressOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.graphhopper.util;

import org.apache.commons.lang3.time.DurationFormatUtils;
import org.apache.commons.lang3.time.StopWatch;

import java.io.IOException;
import java.io.OutputStream;
import java.util.function.Consumer;

class ProgressOutputStream extends OutputStream {
private final OutputStream out;
private final long max;
private final Consumer<String> log;
private final String progressRenderString;

private long current = 0;
private long speed = 0;

private final StopWatch time;
private final StopWatch totalTime;


public ProgressOutputStream(OutputStream out, String task, long max, Consumer<String> log) {
this.out = out;
this.max = max;
this.log = log;
if (max > 0) {
this.progressRenderString = "%s %%%dd/%d (%%3d%%%%) [%%.2f MB/s / %%.2f MB/s] %%s"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider unifying the render string format between the two scenarios when max > 0 and otherwise.
The formatting should be consistent between both strings and use either MB/s (megabytes per second) or Mbps (megabits per second). It should also match the actual calculation in the render method where the values represent megabits per second calculated via speed / (time * 1000.0). Should you, however, decide to settle on MB/s rather than Mbps you might want to adjust the corresponding formula.

.formatted(task, Long.toString(max).length(), max);
} else {
this.progressRenderString = "%s %%d/? [%%.2f Mbps / %%.2f Mbps] %%s".formatted(task);
}

this.time = StopWatch.createStarted();
this.totalTime = StopWatch.createStarted();

}

private void progress(long n) {
current += n;
speed += n;
if (time.getTime() >= 1000) {
triggerLog();
time.reset();
time.start();
speed = 0;
}
}

private void triggerLog() {
log.accept(render((speed) / (Math.max(time.getTime(), 1) * 1000.0), (current) / (Math.max(totalTime.getTime(), 1) * 1000.0)));
}

private String render(double mbits, double mbitsTotal) {
if (max > 0) {
return progressRenderString
.formatted(current, (int) ((current / (double) max) * 100),
mbits, mbitsTotal,
DurationFormatUtils.formatDuration(totalTime.getTime(), "mm:ss.SSS"));
}
return progressRenderString
.formatted(current,
mbits, mbitsTotal,
DurationFormatUtils.formatDuration(totalTime.getTime(), "mm:ss.SSS"));

}

@Override
public void write(byte[] b, int off, int len) throws IOException {
out.write(b, off, len);
progress(len);
}

@Override
public void write(int b) throws IOException {
out.write(b);
progress(1);
}

@Override
public void close() throws IOException {
out.close();
triggerLog();
}
}