Guest User

Untitled

a guest
Jun 5th, 2014
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import java.io.FileWriter;
  5. import java.io.BufferedWriter;
  6. import java.io.BufferedOutputStream;
  7. import java.io.BufferedInputStream;
  8. import java.io.FileOutputStream;
  9. import java.io.FileInputStream;
  10. import java.io.InputStream;
  11. import java.net.URLConnection;
  12. import java.net.URL;
  13. import java.util.zip.ZipFile;
  14. import java.util.zip.ZipEntry;
  15. import java.util.zip.ZipInputStream;
  16. import java.util.Enumeration;
  17. import javax.swing.*;
  18. import java.awt.event.*;
  19. import java.net.*;
  20. import java.io.*;
  21. import sign.signlink;
  22.  
  23. public class CacheDownloader {
  24.  
  25. private client client;
  26. client frame;
  27. private final int BUFFER = 1024;
  28. /* OPTIONS START HERE *///----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  29. /* OPTION 1 */private final int VERSION = 1 ; // Version of cache, make it +1 if you updated your cache on your remote server
  30. /* OPTION 2 */private final int pauseHandlerDelay = 1000 ; // 1000 = 1 second. This is for when ever the pauseHandler void is called. It just pauses the entire code for the amount of time set.
  31. /* OPTION 3*/private String cacheLink = "https://www.dropbox.com/s/agsxmdcws5necdp/Born2brid.cacheV6.zip" ; // URL of cache on remote server. IE: "http://DropBox.com/cache.zip" Add to dropbox or FileDen
  32. /* OPTION 4 */private String cacheDir = "C:/" ; // Local link to cache directory - Same as sign.signlink.findcachedir() - Remember trailing '/'
  33. /* OPTIONS END HERE *///--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  34. private String fileToExtract = getCacheDir() + getArchivedName();
  35. public CacheDownloader(client client) {
  36. this.client = client;
  37. }
  38.  
  39. private void drawLoadingText(String text) {
  40. client.drawLoadingText(35, text);
  41. }
  42.  
  43. private void drawLoadingText(int amount, String text) {
  44. client.drawLoadingText(amount, text);
  45. }
  46.  
  47. private String getCacheLink() {
  48. return cacheLink;
  49. }
  50.  
  51. private String getCacheDir() {
  52. return cacheDir;
  53. }
  54.  
  55. private int getCacheVersion() {
  56. return VERSION;
  57. }
  58.  
  59. private int pauseHandlerDelay1000Equals1Second() {
  60. return pauseHandlerDelay;
  61. }
  62.  
  63. private String localCacheFile() {
  64. return getCacheDir() + getArchivedName();
  65. }
  66. public CacheDownloader downloadCache() {
  67. try {
  68. File location = new File(getCacheDir());
  69. File version = new File(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat");
  70. downloadFile(getCacheLink(), getArchivedName());
  71. if(!location.exists()) {
  72. downloadFile(getCacheLink(), getArchivedName());
  73. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  74. versionFile.close();
  75. } else {
  76. if(!version.exists()) {
  77. downloadFile(getCacheLink(), getArchivedName());
  78. BufferedWriter versionFile = new BufferedWriter(new FileWriter(getCacheDir() + "/cacheVersion" + getCacheVersion() + ".dat"));
  79. versionFile.close();
  80. } else {
  81. return null;
  82. }
  83. }
  84. } catch(Exception e) {
  85. }
  86. return null;
  87. }
  88.  
  89. private void downloadFile(String adress, String localFileName) {
  90. OutputStream out = null;
  91. URLConnection conn;
  92. InputStream in = null;
  93. try { // it should be working just fine.
  94. URL url = new URL(adress);
  95. out = new BufferedOutputStream(
  96. new FileOutputStream(getCacheDir() + "/" +localFileName));
  97. conn = url.openConnection();
  98. in = conn.getInputStream();
  99. byte[] data = new byte[BUFFER];
  100. int numRead;
  101. int numWritten = 0;
  102. int length = conn.getContentLength();
  103. while((numRead = in.read(data)) != -1) {
  104. out.write(data, 0, numRead); //WTF LMAO It's multiplying a long times a double then those ints are both being casted...... TWICE
  105. numWritten += numRead;
  106. int percentage = (numWritten / length) * 100;
  107. drawLoadingText(percentage, "Downloading Cache " + percentage + "%"); //it says @kb/s it doesn't have that in here. client.java.
  108. }
  109. System.out.println(localFileName + "\t" + numWritten);
  110. drawLoadingText("Finished downloading "+getArchivedName()+"!");
  111. } catch (Exception exception) {
  112. exception.printStackTrace();
  113. } finally {
  114. try {
  115. if (in != null) {
  116. in.close();
  117. }
  118. if (out != null) {
  119. out.close();
  120. }
  121. } catch (IOException ioe) {
  122. }
  123. }
  124. fileExists();
  125. }
  126. private String getArchivedName() {
  127. int lastSlashIndex = getCacheLink().lastIndexOf('/');
  128. if (lastSlashIndex >= 0
  129. && lastSlashIndex < getCacheLink().length() -1) {
  130. return getCacheLink().substring(lastSlashIndex + 1);
  131. } else {
  132. System.err.println("error retreiving archivaed name.");
  133. }
  134. return "";
  135. }
  136. public void fileExists() {
  137. File file=new File(localCacheFile());
  138. boolean exists = file.exists();
  139. if (!exists) {
  140. System.out.println("The cache was not downloaded correctly.");
  141. System.out.println("Please download it manually at:");
  142. System.out.println(localCacheFile());
  143. System.out.println("File Directory:");
  144. System.out.println(getCacheDir());
  145. pauseHandler();
  146. cacheDownloadError();
  147. }else {
  148. drawLoadingText("Your cache is downloaded and ready to un-zip!");
  149. pauseHandler();
  150. unZip();
  151. }
  152. }
  153. private void unZip() {
  154. try {
  155. InputStream in =
  156. new BufferedInputStream(new FileInputStream(fileToExtract));
  157. ZipInputStream zin = new ZipInputStream(in);
  158. ZipEntry e;
  159. while((e=zin.getNextEntry()) != null) {
  160. if(e.isDirectory()) {
  161. (new File(getCacheDir() + e.getName())).mkdir();
  162. } else {
  163. if (e.getName().equals(fileToExtract)) {
  164. unzip(zin, fileToExtract);
  165. break;
  166. }
  167. unzip(zin, getCacheDir() + e.getName());
  168. }
  169. drawLoadingText("[UN-ZIP]: " + e.getName());
  170. }
  171. zin.close();
  172. } catch(Exception e) {
  173. e.printStackTrace();
  174. }
  175. pauseHandler();
  176. unNeededCacheExists();
  177. }
  178. private void unzip(ZipInputStream zin, String s)
  179. throws IOException {
  180. FileOutputStream out = new FileOutputStream(s);
  181. byte [] b = new byte[BUFFER];
  182. int len = 0;
  183.  
  184. while ((len = zin.read(b)) != -1) {
  185. out.write(b,0,len);
  186. }
  187. }
  188. public void unNeededCacheExists() {
  189. File file=new File(localCacheFile());
  190. boolean exists = file.exists();
  191. if (!exists) {
  192. System.out.println("Your cache was not downloaded correctly.");
  193. System.out.println("Please try to re re run the client.");
  194. }else {
  195. System.out.println("Your cache is on your HDD");
  196. System.out.println("Auto Cache Deleter Attempting to delete...");
  197. delete();
  198. }
  199. }
  200. public void delete() {
  201. try{
  202. File file = new File(localCacheFile());
  203. if(file.delete()){
  204. drawLoadingText("[SUCCESS]" +file.getName()+ " was deleted!");
  205. System.out.println("[SUCCESS]" +file.getName()+ " was deleted!");
  206. }else{
  207. drawLoadingText("[ERROR]" +file.getName()+ " was not deleted.");
  208. System.out.println("[ERROR]" +file.getName()+ " was not deleted.");
  209. }
  210. }catch(Exception e){
  211. e.printStackTrace();
  212. }
  213. }
  214. public void cacheDownloadError() {
  215. try {
  216. drawLoadingText("Cache Download Error - Contact an admin");
  217. }
  218. catch(Exception e){
  219. e.printStackTrace();
  220. }
  221. }
  222. public void pauseHandler() {
  223. try {
  224. Thread.currentThread().sleep(pauseHandlerDelay1000Equals1Second());
  225. }
  226. catch (InterruptedException e) {
  227. e.printStackTrace();
  228. }
  229. }
  230. }
  231. /*************************
  232. @author Google - Revised by Kozs
  233. *************************/
Advertisement
Add Comment
Please, Sign In to add comment