turt2live

Untitled

Dec 10th, 2011
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.37 KB | None | 0 0
  1. /* meaSuite is copyright 2011/2012 of Turt2Live Programming and Sayshal Productions
  2.  *
  3.  * Modifications of the code, or any use of the code must be preauthorized by Travis
  4.  * Ralston (Original author) before any modifications can be used. If any code is
  5.  * authorized for use, this header must retain it's original state. The authors (Travis
  6.  * Ralston and Tyler Heuman) can request your code at any time. Upon code request you have
  7.  * 24 hours to present code before we will ask you to not use our code.
  8.  *
  9.  * Contact information:
  10.  * Travis Ralston
  11.  *
  12.  * Tyler Heuman
  13.  */
  14. package mea.External;
  15.  
  16. import java.io.*;
  17. import java.net.*;
  18.  
  19. import mea.Logger.MeaLogger;
  20.  
  21. public class Download {
  22.     public static double downloadSize = 0;
  23.     public static double downloadAmount = 0;
  24.     public static int lastDownloadPercent = 0;
  25.     private File log = new File("/meaLogger/data_transfer.txt");
  26.     public static String getDownloadStatus(){
  27.         double pert = (downloadAmount/downloadSize)*100;
  28.         int percent = (int) pert;
  29.         return "Downloaded "+downloadAmount+" bytes of "+downloadSize+" bytes. "+percent+"%";
  30.     }
  31.     public static int getDownloadPercent(){
  32.         return (int) ((downloadAmount/downloadSize)*100);
  33.     }
  34.     public int getDownloadStatusInt(){
  35.         if(downloadAmount==downloadSize){
  36.             return 1;
  37.         }else{
  38.             return 0;
  39.         }
  40.     }
  41.     public static int getFileSize() throws Exception{
  42.         FileInputStream fstream = new FileInputStream("filesize.txt");
  43.         DataInputStream in = new DataInputStream(fstream);
  44.             BufferedReader br = new BufferedReader(new InputStreamReader(in));
  45.         String strLine;
  46.         int retValue = 0;
  47.         while ((strLine = br.readLine()) != null)   {
  48.          retValue = Integer.parseInt(strLine);
  49.         }
  50.         in.close();
  51.         return retValue;
  52.     }
  53.    
  54.      public Download(URL url, String filename, boolean println) throws Exception {
  55.             URLConnection con;  // represents a connection to the url we want to dl.
  56.             DataInputStream dis;  // input stream that will read data from the file.
  57.             FileOutputStream fos; //used to write data from inut stream to file.
  58.             byte[] fileData;  //byte aray used to hold data from downloaded file.
  59.             try {
  60.                 con = url.openConnection(); // open the url connection.
  61.                 dis = new DataInputStream(con.getInputStream()); // get a data stream from the url connection.
  62.                 fileData = new byte[con.getContentLength()]; // determine how many byes the file size is and make array big enough to hold the data
  63.                 //System.out.println("Getting URL and file information...");
  64.                 downloadSize = fileData.length;
  65.                 int t1 = 0;
  66.                // Thread.sleep(2000);
  67.                 for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
  68.                     fileData[x] = dis.readByte();
  69.                     if(t1==0){
  70.                         t1=1;
  71.                         //System.out.println("Downloading File. This may take a while.");
  72.                     }
  73.                     downloadAmount = x+1;
  74.                     if(getDownloadPercent()== 100){
  75.                         String parts[] = filename.split("\\/");
  76.                         String file = parts[parts.length-2];
  77.                         if(println){
  78.                             System.out.println(file+" :: "+getDownloadStatus());
  79.                             MeaLogger.log(file+" :: "+getDownloadStatus(), log);
  80.                         }
  81.                         lastDownloadPercent = getDownloadPercent();
  82.                     }
  83.                 }
  84.                 dis.close(); // close the data input stream
  85.               //  System.out.println("Finishing download....");
  86.                 //Thread.sleep(900);
  87.                 fos = new FileOutputStream(new File(filename));  //create an object representing the file we want to save
  88.                 fos.write(fileData);  // write out the file we want to save.
  89.                 fos.close(); // close the output stream writer
  90.                 //System.out.println("Download Complete.");
  91.             }
  92.             catch(MalformedURLException m) {
  93.                 System.out.println(m);
  94.             }
  95.             catch(IOException io) {
  96.                 System.out.println(io);
  97.             }
  98.         }
  99.    
  100.     public Download(URL url, String filename) throws Exception {
  101.         URLConnection con;  // represents a connection to the url we want to dl.
  102.         DataInputStream dis;  // input stream that will read data from the file.
  103.         FileOutputStream fos; //used to write data from inut stream to file.
  104.         byte[] fileData;  //byte aray used to hold data from downloaded file.
  105.         try {
  106.             con = url.openConnection(); // open the url connection.
  107.             dis = new DataInputStream(con.getInputStream()); // get a data stream from the url connection.
  108.             fileData = new byte[con.getContentLength()]; // determine how many byes the file size is and make array big enough to hold the data
  109.             //System.out.println("Getting URL and file information...");
  110.             downloadSize = fileData.length;
  111.             int t1 = 0;
  112.            // Thread.sleep(2000);
  113.             for (int x = 0; x < fileData.length; x++) { // fill byte array with bytes from the data input stream
  114.                 fileData[x] = dis.readByte();
  115.                 if(t1==0){
  116.                     t1=1;
  117.                     //System.out.println("Downloading File. This may take a while.");
  118.                 }
  119.                 downloadAmount = x+1;
  120.                 if(getDownloadPercent()== 100){
  121.                     String parts[] = filename.split("\\/");
  122.                     String file = parts[parts.length-2];
  123.                     System.out.println(file+" :: "+getDownloadStatus());
  124.                     lastDownloadPercent = getDownloadPercent();
  125.                 }
  126.             }
  127.             dis.close(); // close the data input stream
  128.           //  System.out.println("Finishing download....");
  129.             //Thread.sleep(900);
  130.             fos = new FileOutputStream(new File(filename));  //create an object representing the file we want to save
  131.             fos.write(fileData);  // write out the file we want to save.
  132.             fos.close(); // close the output stream writer
  133.             //System.out.println("Download Complete.");
  134.         }
  135.         catch(MalformedURLException m) {
  136.             System.out.println(m);
  137.         }
  138.         catch(IOException io) {
  139.             System.out.println(io);
  140.         }
  141.     }
  142. }
  143.  
  144.  
Advertisement
Add Comment
Please, Sign In to add comment