Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (580)
games submitted by our members
Games in WIP (500)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: GabrielBailey74 (posted 2013-06-08 18:02:25, viewed 177 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194  
package project.core;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.text.DecimalFormat;

public class Launcher {

   // File URLS[] for test downloading.
   private static final String[] FILE_URLS = {
         // Big Archive (181MB)
         "http://199.91.153.141/w7i42ti034bg/i5gwc4dsihggdde/Project.rar",
         // Small Archive (18.31MB)
         "http://205.196.123.192/474cdepm76cg/4wj3rxv7hj7jvsc/1-Siccmade-Client.rar"
   };
   
   // The current Index of the URL[] array to download.
   private static final int URL_INDEX = 0;
   
   // The Name of the allocated downloaded file.
   private static final String DOWNLOADED_FILE_NAME = "download.rar";
   
   // The Allocated downloaded file.
   private static File DOWNLOADED_FILE = null;
   
   // ByteBuffer for reading bytes.
   private static byte[] BYTE_BUFFER = new byte[1024];
   
   // Formatter for formatting hrs, min, sec.
   private static DecimalFormat formatter = new DecimalFormat("##.##");
   
   /*
    * Main entry point into the application.
    */
   public static void main(String[] args) throws IOException{
      int inCount;
      double length = Util.getURLSizeInKB(FILE_URLS[URL_INDEX]);
      double numWritten = 0;
      int percentage = 0;
      int kbs = 0;
      double eta = 0.0;
      int lastPercentage = 0;
      int lastKbs = 0;
      int minutes = 0, seconds = 0;
      int hours = 0;
      boolean finished = false;
      boolean noTimeLeft = (hours == 0 && minutes == 0 && seconds == 0);
      /*
       * Notify the user we're beginning the download.
       */
      log("Starting File download...");
      log("Download size: " + Util.getURLSizeInMB(FILE_URLS[URL_INDEX]) + "MB.");

      /*
       * Initialize the Input Stream. (Downloading from remote system / server)
       */
      BufferedInputStream in = new BufferedInputStream(new URL(FILE_URLS[URL_INDEX]).openStream());
      
      /*
       * Initialize the Output Stream. (Downloading on local system / pc)
       */
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(DOWNLOADED_FILE_NAME)));
            
      /*
       * Keeping track of when we started.
       */
      long startTime = System.currentTimeMillis();
      
      /*
       * Begin the download.
       */
      while ((inCount = in.read(BYTE_BUFFER, 0, BYTE_BUFFER.length)) != -1) {
         /*
          * Write out the bytes read from the connection.
          * (Amount read per loop is determined by the Byte_Buffers size.
          */
         out.write(BYTE_BUFFER, 0, inCount);
         numWritten += inCount;
         
         /*
          * Update the previous percentage/kbs to the current.
          */
         lastPercentage = percentage;
         lastKbs = kbs;
         
         /*
          * Calculate the Percentage.
          */
         percentage = (int) (((double) numWritten / (double) length) * 100D);
         
         /*
          * Calculate the KBS.
          */
         kbs = (int) ((numWritten / BYTE_BUFFER.length) / (1 + ((System.currentTimeMillis() - startTime) / 1000)));
         
         /*
          * Calculate the ETA.
          */
         eta = (length - numWritten) / kbs / 1000D;
         
         /*
          * Calculate the elapsed:
          * Hours, Minutes, Seconds.
          */
         minutes = Integer.valueOf(formatter.format((int) eta / 60));
         seconds = Integer.valueOf(formatter.format((int) eta));
         hours = Integer.valueOf(formatter.format((int) minutes / 60));

         /*
          * Format the ETA:
          * Hours, Minutes, Seconds.
          */
         String etaFormatted = String.valueOf(minutes);
         if (hours > 1) {
            etaFormatted = hours+" hours";
         } else if (hours == 1) {
            etaFormatted = hours+" hour, "+minutes+" minutes";
         } else if (hours == 0 && minutes > 0) {
            String tmp = "";
            if (minutes == 1) {
               tmp = "minute";
            } else if (minutes > 1) {
               tmp = "minutes";
            }
            etaFormatted = minutes+" "+tmp;
         } else if (hours == 0 && minutes == 0 && seconds > 0) {
            String tmp = "";
            if (seconds == 1) {
               tmp = "second";
            } else if (seconds > 1) {
               tmp = "seconds";
            }
            etaFormatted = seconds+" "+tmp;
         }

         /*
          * Display some progress about the download
          * that's currently running only if our
          * current download percentage has increased from the last.
          */
         if (percentage == lastPercentage) {
            final Double allocatedDownloadData = Double.valueOf(formatter.format(Util.getFileSizeInMB(DOWNLOADED_FILE_NAME)));
            final Double totalDataToAllocate = Double.valueOf(Util.getURLSizeInMB(FILE_URLS[URL_INDEX]));
            String[] data = { 
                  "Downloading - " + percentage + "% || @ " + kbs+ " Kb/s || ETA: "+etaFormatted+"",
                  allocatedDownloadData+"MB / "+totalDataToAllocate+"MB."
            };
            log(data[0]+" || "+data[1]);
         }
      }
      /*
       * Close the Input/Output streams.
       */
      out.flush();
      out.close();
      in.close();
      
      /*
       * Check if the download is complete.
       * Compare the size of the file on the remote server
       * to the file downloaded on the host's machine.
       */
      if (noTimeLeft && percentage == 100 && !finished) {
         log("Download finished, checking for data loss...");
         final Double urlSize = (double) Math.round(Double.valueOf(Util.getURLSizeInKB(FILE_URLS[URL_INDEX])));
         final Double downloadedFileSize = (double) Math.round(Double.valueOf(Util.getFileSizeInKB(DOWNLOADED_FILE_NAME)));
         if (downloadedFileSize.equals(urlSize)) {
            log("No data loss, download was successful.");
         } else {
            log("Data loss detected: (URL SIZE: "+urlSize+" KB, DOWNLOAD SIZE: "+downloadedFileSize+" KB).");
         }
         finished = true;
      }
      
      /*
       * Allocate the downloaded file and
       * display some statistics.
       */
      if (finished) {
         DOWNLOADED_FILE = new File(DOWNLOADED_FILE_NAME);
         log("Downloaded File size: "+ Util.getFileSizeInMB(DOWNLOADED_FILE_NAME) + "MB.");
         log("Elapsed time: "+(System.currentTimeMillis() - startTime) / 1000+" seconds");
      }
   }

   private static void log(String m) {
      System.out.println("[Downloader]: " + m);
   }

}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (48 views)
2014-04-15 18:08:23

BurntPizza (44 views)
2014-04-15 03:46:01

UprightPath (60 views)
2014-04-14 17:39:50

UprightPath (42 views)
2014-04-14 17:35:47

Porlus (58 views)
2014-04-14 15:48:38

tom_mai78101 (81 views)
2014-04-10 04:04:31

BurntPizza (140 views)
2014-04-08 23:06:04

tom_mai78101 (240 views)
2014-04-05 13:34:39

trollwarrior1 (200 views)
2014-04-04 12:06:45

CJLetsGame (207 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!