1010import java .net .URLConnection ;
1111import java .nio .channels .Channels ;
1212import java .nio .channels .ReadableByteChannel ;
13+ import java .util .jar .JarFile ;
1314import java .util .jar .Manifest ;
1415
1516import javax .swing .JFileChooser ;
2223
2324public class SortAlgorithmVisualizer {
2425 private static final String SERVER_URL = "http://littlerolf.github.io/SortAlgorithmVisualizer/" ;
26+ private static final File DOWNLOADED_FILE = new File (
27+ System .getProperty ("user.home" ) + File .separator + ".sav"
28+ + File .separator + "SAV.jar" );
2529
2630 public static void main (String [] args ) {
27- if (getLocalVersion () > -1 ) {
28- if (isRemoteNewer ()) {
29- startJar (downloadRemoteJar ().getAbsolutePath ());
30- } else {
31- startLocal ();
32- }
33- } else {
31+ int remoteVersion = getRemoteVersion ();
32+ int localVersion = getLocalVersion ();
33+ int downloadedVersion = getDownloadedJarVersion ();
34+
35+ if (remoteVersion > downloadedVersion && remoteVersion > localVersion )
36+ startJar (downloadRemoteJar ().getAbsolutePath ());
37+ else if (downloadedVersion > remoteVersion
38+ && downloadedVersion > localVersion )
39+ startJar (DOWNLOADED_FILE .getAbsolutePath ());
40+ else
3441 startLocal ();
35- }
42+ /*
43+ * if (getLocalVersion() > -1) { if (isRemoteNewer()) {
44+ * startJar(downloadRemoteJar().getAbsolutePath()); } else {
45+ * startLocal(); } } else { startLocal(); }
46+ */
3647 }
3748
3849 private static int getLocalVersion () {
@@ -53,6 +64,24 @@ private static int getLocalVersion() {
5364 return -1 ;
5465 }
5566
67+ private static int getDownloadedJarVersion () {
68+ if (!DOWNLOADED_FILE .exists ())
69+ return -1 ;
70+
71+ try {
72+ JarFile f = new JarFile (DOWNLOADED_FILE );
73+ int version = Integer .valueOf (f .getManifest ().getMainAttributes ()
74+ .getValue ("Build-Number" ));
75+ f .close ();
76+ System .out .println ("Downloaded version is " + version + "." );
77+ return version ;
78+ } catch (IOException e ) {
79+ e .printStackTrace ();
80+ } catch (NullPointerException e ) {
81+ }
82+ return -1 ;
83+ }
84+
5685 private static int getRemoteVersion () {
5786 URL url = null ;
5887 try {
@@ -91,8 +120,7 @@ private static int getRemoteVersion() {
91120
92121 private static File downloadRemoteJar () {
93122 System .out .println ("Downloading remote jar." );
94- File f = new File (System .getProperty ("user.home" ) + File .separator
95- + ".sav" + File .separator + "SAV.jar" );
123+ File f = DOWNLOADED_FILE ;
96124 f .getParentFile ().mkdirs ();
97125 URL website ;
98126 try {
0 commit comments