11package fr .formiko .utils ;
22
3+ import fr .formiko .utils .progressions .FLUProgression ;
34import java .io .File ;
45import java .io .IOException ;
56import java .io .InputStream ;
2223 *
2324 * @author Hydrolien
2425 * @since 0.0.3
25- * @version 0.0.3
26+ * @version 0.0.4
2627 */
2728public class FLUFiles {
2829 private static FLUFilesInternal internal = new FLUFilesInternal ();
30+ private static FLUProgression progression ;
2931
3032 private FLUFiles () {} // hide constructor
3133
34+ // GET SET ----------------------------------------------------------------------
35+ public static FLUProgression getProgression () { return progression ; }
36+ public static void setProgression (FLUProgression progression ) { FLUFiles .progression = progression ; }
37+
3238 public static boolean isAValidePath (String path ) { return internal .isAValidePath (path ); }
3339 public static boolean createFile (String path ) { return internal .createFile (path ); }
3440 public static boolean createDirectory (String path ) { return internal .createDirectory (path ); }
@@ -50,8 +56,7 @@ public static boolean unzip(String source, String destination, String directoryI
5056 }
5157 public static boolean unzip (String source , String destination ) { return unzip (source , destination , "" ); }
5258
53- public static boolean download (String url , String destination , boolean withProgressInfo ) { return false ; }
54- public static boolean download (String url , String destination ) { return download (url , destination , false ); }
59+ public static boolean download (String url , String destination ) { return internal .download (url , destination ); }
5560 public static boolean downloadAndUnzip (String url , String destination , String directoryInsideZipToGet ) { return false ; }
5661 public static boolean downloadAndUnzip (String url , String destination ) { return downloadAndUnzip (url , destination , "" ); }
5762 public static String downloadAndRead (String url ) { return null ; }
@@ -325,7 +330,82 @@ private void createZipEntry(String destination, String directoryInsideZipToGet,
325330 Files .copy (zis , Paths .get (absoluteDestinationPath + entryName ));
326331 }
327332 }
333+ }
334+ }
335+
336+ private boolean download (String url , String destination ) {
337+ if (isAValidePath (url ) && isAValidePath (destination )) {
338+ try {
339+ URL urlObject = URI .create (url ).toURL ();
340+ InputStream is = urlObject .openStream ();
341+ File destinationFile = new File (destination );
342+ createParents (destinationFile );
343+ Files .copy (is , destinationFile .toPath ());
344+ return true ;
345+ } catch (IOException e ) {
346+ return false ;
347+ }
348+ } else {
349+ return false ;
350+ }
351+ }
352+ }
353+
354+ class FLUProgressionThread extends Thread {
355+ private File fileOut ;
356+ private long fileToDowloadSize ;
357+ private boolean running ;
358+ private String downloadName ;
359+ private FLUProgression progressionInstance ;
360+ /**
361+ * {@summary Main constructor.}<br>
362+ *
363+ * @param fileOut file that we are curently filling by the downloading file
364+ * @param fileToDowloadSize size that we should reach when download will end
365+ */
366+ public FLUProgressionThread (File fileOut , long fileToDowloadSize , String downloadName , FLUProgression progressionInstance ) {
367+ this .fileOut = fileOut ;
368+ this .fileToDowloadSize = fileToDowloadSize ;
369+ this .downloadName = downloadName ;
370+ this .progressionInstance = progressionInstance ;
371+ running = true ;
372+ }
373+
374+ public void stopRuning () { running = false ; }
375+ /**
376+ * {@summary Main function that print every second %age of download done.}<br>
377+ */
378+ public void run () {
379+ long fileOutSize = 0 ;
380+ long lastFileOutSize = 0 ;
381+ long timeStart = System .currentTimeMillis ();
382+ long timeFromLastBitDownload = timeStart ;
383+ while (fileOutSize < fileToDowloadSize && running ) {
384+ fileOutSize = fileOut .length ();
385+ double progression = ((double ) fileOutSize ) / (double ) fileToDowloadSize ;
386+ int percent = (int ) (100 * progression );
387+ long curentTime = System .currentTimeMillis ();
388+ long timeElapsed = curentTime - timeStart ;
389+ long timeLeft = (long ) ((double ) ((timeElapsed / progression ) - timeElapsed ));
390+ String sTimeLeft = FLUTime .msToTime (timeLeft ) + " left" ;
391+ String message = "Downloading " + downloadName + " - " + percent + "% - " ;
392+ if (fileOutSize != lastFileOutSize ) {// update watcher of working download
393+ timeFromLastBitDownload = curentTime ;
394+ }
395+ if (timeFromLastBitDownload + 10000 < curentTime ) {
396+ message += (((curentTime - timeFromLastBitDownload ) / 1000 ) + "s untill a new bit haven't been download" );
397+ if (timeFromLastBitDownload + 60000 < curentTime ) {
398+ stopRuning ();
399+ // TODO stop the IO opperation.
400+ }
401+ } else {
402+ message += sTimeLeft ;
403+ }
404+ progressionInstance .setDownloadingValue (percent );
405+ progressionInstance .setDownloadingMessage (message );
328406
407+ lastFileOutSize = fileOutSize ;
408+ FLUTime .sleep (50 );
329409 }
330410 }
331411 }
0 commit comments