bandwith wip

This commit is contained in:
Marc BARBIER
2021-07-09 17:36:08 +02:00
parent c64ddd90c5
commit 47d9e8e392
2 changed files with 60 additions and 3 deletions
+19 -2
View File
@@ -1,13 +1,30 @@
package downloader; package downloader;
import java.io.PrintStream;
public class Log { public class Log {
private Log() {} private Log() {}
private static String staticLine = "";
private static PrintStream lastStream = System.out;
public static void e(String who, String msg) { public static void e(String who, String msg) {
System.out.println(String.valueOf(who) + " : " + msg); printNonStatic(String.valueOf(who) + " : " + msg, System.err);
} }
public static void i(String who, String msg) { public static void i(String who, String msg) {
if(Main.verbose)System.out.println(String.valueOf(who) + " : " + msg); if(Main.verbose) printNonStatic(String.valueOf(who) + " : " + msg, System.out);
}
private static void printNonStatic(String line, PrintStream stream) {
//erase last stream since we used \r
lastStream.print("");
stream.println(line);
stream.print(staticLine + "\r");
lastStream = stream;
}
public static void setStaticPrint(String staticString) {
staticLine = staticString;
System.out.print(staticString + "\r");
} }
} }
@@ -10,6 +10,7 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Locale;
import downloader.Log; import downloader.Log;
@@ -111,6 +112,7 @@ public class HttpHelper {
// imposible vu qu'on l'a crée precedament // imposible vu qu'on l'a crée precedament
} }
double start = System.currentTimeMillis();
try (InputStream in = con.getInputStream()) { try (InputStream in = con.getInputStream()) {
// on lit par tranche de 4mo // on lit par tranche de 4mo
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
@@ -119,6 +121,15 @@ public class HttpHelper {
int bytesRead = in.read(buffer); int bytesRead = in.read(buffer);
if (bytesRead < 0) if (bytesRead < 0)
break; break;
{
double elapsed = ( System.currentTimeMillis() - start ) / 1000;
if(elapsed != 0) {
long estimatedBandwith = (long) (bytesRead / elapsed);
//display the estimate
Log.setStaticPrint(formatBandwidth(estimatedBandwith));
start = System.currentTimeMillis();
}
}
downloadingWriter.write(buffer, 0, bytesRead); downloadingWriter.write(buffer, 0, bytesRead);
} }
downloadingWriter.close(); downloadingWriter.close();
@@ -128,6 +139,35 @@ public class HttpHelper {
} }
/**
*
* @param estimatedBandwith in o/s
* @return
*/
private static String formatBandwidth(long estimatedBandwith) {
//is ko/s
if(estimatedBandwith > 1000) {
//is mo/s
if(estimatedBandwith > 1000 * 1000) {
return String.valueOf(estimatedBandwith/ (1000 * 1000)) + "M" + getLocalizedByte() + "/s";
} else {
return String.valueOf(estimatedBandwith/ 1000) + "K" + getLocalizedByte() + "/s";
}
} else {
return String.valueOf(estimatedBandwith) + getLocalizedByte() + "/s";
}
}
private static String getLocalizedByte() {
Locale l = Locale.getDefault();
if (l.equals(Locale.FRANCE) | l.equals(Locale.FRENCH)) {
return "O";
} else {
return "B";
}
}
public static String getFileNameFromURL(String url) public static String getFileNameFromURL(String url)
{ {
String[] urlsplit = url.split("/"); String[] urlsplit = url.split("/");