public static Download downloadFile( String url, String output, String cacheName, String md5, DownloadListener listener) throws IOException { int tries = Settings.getLoginTries(); File outputFile = null; Download download = null; while (tries > 0) { System.out.println("Starting download of " + url + ", with " + tries + " tries remaining"); tries--; download = new Download(url, output); download.setListener(listener); download.run(); if (!download.isSuccess()) { if (download.getOutFile() != null) { download.getOutFile().delete(); } System.err.println("Download of " + url + " Failed!"); if (listener != null) listener.stateChanged("Download Failed, retries remaining: " + tries, 0F); } else { if (md5 != null) { String resultMD5 = MD5Utils.getMD5(download.getOutFile()); System.out.println("Expected MD5: " + md5 + " Calculated MD5: " + resultMD5); if (resultMD5.equals(md5)) { outputFile = download.getOutFile(); break; } } else { outputFile = download.getOutFile(); break; } } } if (outputFile == null) { throw new IOException("Failed to download " + url); } if (cacheName != null) { Utils.copy(outputFile, new File(Launcher.getGameUpdater().getBinCacheDir(), cacheName)); } return download; }
public static void updateSpoutcraftYMLCache() { if (!updated) { synchronized (key) { String urlName = MirrorUtils.getMirrorUrl("spoutcraft.yml", "http://get.spout.org/spoutcraft.yml", null); if (urlName != null) { try { int selected = -1; if (spoutcraftYML.exists()) { try { YAMLProcessor config = new YAMLProcessor(spoutcraftYML, false); config.load(); selected = config.getInt("current", -1); } catch (Exception ex) { ex.printStackTrace(); } } URL url = new URL(urlName); HttpURLConnection con = (HttpURLConnection) (url.openConnection()); System.setProperty("http.agent", ""); con.setRequestProperty( "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.100 Safari/534.30"); Utils.copy(con.getInputStream(), new FileOutputStream(spoutcraftYML)); YAMLProcessor config = new YAMLProcessor(spoutcraftYML, false); config.load(); config.setProperty("current", selected); config.setProperty("launcher", Main.getBuild("launcher-version")); config.save(); } catch (IOException e) { e.printStackTrace(); } } updated = true; } } }