Witam Nie jestem Specem od Javy temu kieruje do Was pytanie :
Moze mi ktoś omówić w skrócie jak działa ten kod , na podstawie pobierania plików aktualizacji ze strony
był bym strasznie wdzieczny
package org.spoutcraft.update;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLClassLoader;
public class Core
{
public static void main(String[] args)
throws InterruptedException
{
File spoutcraftDir = PlatformUtils.getWorkingDirectory();
spoutcraftDir.mkdirs();
File versionFile = new File(spoutcraftDir, "launcherVersion");
if (!spoutcraftDir.exists()) spoutcraftDir.mkdirs();
String latestVersion = null;
String currenVersion = null;
try {
latestVersion = getVersion();
} catch (Exception localException1) {
}
try {
currenVersion = getCurrentVersion(versionFile);
}
catch (Exception localException2) {
}
File jar = new File(spoutcraftDir, "Spoutcraft-Launcher.jar");
if (!jar.exists()) {
if (latestVersion == null) {
System.out.print("Theres something wrong, we couldn't get internet... \n Stopping...");
System.exit(0);
}
try
{
downloadFile("http://ci.getspout.org/job/Spoutcraft%20Launcher/promotion/latest/Recommended/artifact/target/launcher-dev-SNAPSHOT.jar", jar.getPath());
writeFile(versionFile.getPath(), latestVersion);
} catch (Exception e) {
System.out.print("Theres something wrong, we couldn't get internet... \n Stopping...");
writeFile(versionFile.getPath(), latestVersion);
System.exit(0);
}
}
else if ((latestVersion != null) && (currenVersion != null))
{
if (checkUpdate(currenVersion, latestVersion)) {
try
{
downloadFile("http://ci.getspout.org/job/Spoutcraft%20Launcher/promotion/latest/Recommended/artifact/target/launcher-dev-SNAPSHOT.jar", jar.getPath());
writeFile(versionFile.getPath(), latestVersion);
} catch (Exception e) {
System.out.print("Theres something wrong, we had issues downloading... \n Stopping...");
System.exit(0);
}
}
}
if (!versionFile.exists()) {
jar.delete();
try {
downloadFile("http://ci.getspout.org/job/Spoutcraft%20Launcher/promotion/latest/Recommended/artifact/target/launcher-dev-SNAPSHOT.jar", jar.getPath());
writeFile(versionFile.getPath(), latestVersion);
} catch (Exception e) {
System.out.print("Theres something wrong, we had issues downloading... \n Stopping...");
System.exit(0);
}
}
URL[] urls = new URL[1];
try
{
urls[0] = jar.toURI().toURL();
}
catch (MalformedURLException e) {
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
ClassLoader classLoad = new URLClassLoader(urls);
Class mainClass = null;
try {
mainClass = classLoad.loadClass("org.spoutcraft.launcher.Main");
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
catch (SecurityException e) {
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
try
{
mainClass.newInstance();
}
catch (InstantiationException e)
{
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
catch (IllegalAccessException e) {
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
catch (Exception e) {
e.printStackTrace();
System.out.print("Theres something wrong... \n Stopping...");
System.exit(0);
}
}
private static void downloadFile(String url, String outPut) throws Exception
{
BufferedInputStream in = new BufferedInputStream(new URL(url).openStream());
FileOutputStream fos = new FileOutputStream(outPut);
BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
byte[] data = new byte[1024];
int x = 0;
while ((x = in.read(data, 0, 1024)) >= 0)
{
bout.write(data, 0, x);
}
bout.close();
in.close();
}
public static void writeFile(String out, String contents) {
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter(out);
writer = new BufferedWriter(fWriter);
System.out.print(contents);
writer.write(contents);
writer.close();
}
catch (Exception localException)
{
}
}
public static String getCurrentVersion(File file)
{
try {
FileInputStream fstream = new FileInputStream(file);
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
if ((strLine = br.readLine()) != null) {
return strLine;
}
in.close();
} catch (Exception e) {
return null;
}
return null;
}
public static String getVersion() throws Exception {
String version = "-1";
URL url = new URL("http://ci.getspout.org/job/Spoutcraft%20Launcher/Recommended/buildNumber");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
if ((str = in.readLine()) != null) {
version = str;
return version;
}
in.close();
return null;
}
public static boolean checkUpdate(String current, String latest)
{
int c = Integer.parseInt(current);
int l = Integer.parseInt(latest);
return c < l;
}
}