Checking for update v1

Discussion in 'Resources' started by oyasunadev, Oct 8, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    oyasunadev

    Hello again! This is a replacement for my AutoUpdating technique. This only includes how you would check for the latest version and match with the current. The actual updating technique can be found here. Here it is!

    VersionNumber - Class for handling version numbers.
    VersionNumber (open)
    PHP:
    public class VersionNumber
    {
            List<
    Integerversion = new ArrayList<Integer>();

            public 
    VersionNumber(String version)
            {
                    
    this.version.add(Integer.valueOf(version.split("\\.")[0]));
                    
    this.version.add(Integer.valueOf(version.split("\\.")[1]));
                     
    this.version.add(Integer.valueOf(version.split("\\.")[2]));
            }
    }
    readURL(String url) - Returns each line in a List.
    readURL(String url) (open)
    PHP:
    public List<StringreadURL(String url)
    {
            try {
                    
    URL site = new URL(url);
                    
    URLConnection urlC site.openConnection();
                    
    BufferedReader in = new BufferedReader(new InputStreamReader(urlC.getInputStream()));

                    List<
    Stringlines = new ArrayList<String>();
                    
    String line;
                    while((
    line in.readLine()) != null)
                    {
                            
    lines.add(line);
                    }

                    
    in.close();

                    return 
    lines;
            } catch(
    MalformedURLException e) {
                    
    e.printStackTrace();
            } catch(
    IOException e) {
                    
    e.printStackTrace();
            }

            return 
    null;
    }

    Usage
    Usage (open)
    PHP:
    PluginDescriptionFile pdfFile getDescription();

    VersionNumber currentVersion = new VersionNumber(pdfFile.getVersion());
    List<
    StringversionURL readURL("http://website.com/latest.html");
    String lVersion versionURL.get(0) + "." versionURL.get(1) + "." versionURL.get(2);
    VersionNumber latestVersion = new VersionNumber(lVersion);

    if(
    currentVersion.version[0] < latestVersion.version[0] ||
                    
    currentVersion.version[1] < latestVersion.version[1] ||
                    
    currentVersion.version[2]< latestVersion.version[2])
    {
            
    //UPDATE AVAILABLE
    } else {
            
    //UP TO DATE
    }
    latest.html
    Code:
    (Major)
    (Minor)
    (Build)
    
    Ex:
    1
    0
    0
     
    xboxhacks and Don Redhorse like this.
  2. Offline

    garbagemule

    Just to be completely nitpicky, and assuming that the compiler doesn't optimize the code, the version number thing will not only not work, it can also be slightly generalized and improved. It won't work, because the field isn't initialized, and it's "slow" because you use a regex split for every part of the version. "Big deal", you might say, but I think it's a good idea to make "tutorialy" or "guidy" articles as proper as possible, such that beginners don't fall into bad habits. Anyway, here's my modified version, which allows for an arbitrary amount of sub-versions:
    Code:
    public class VersionNumber
    {
        int[] version;
    
        public VersionNumber(String version)
        {
            // Split the input string by periods.
            String[] parts = version.split("\\.");
    
            // Initialize the field.
            this.version = new int[parts.length];
    
            // Populate the field.
            for (int i = 0; i < parts.length; i++)
                this.version[i] = Integer.valueOf(parts[i]);
        }
    }
    Of course, this is still going to throw a big fat NumberFormatException if the version contains any non-numerical characters (in which case the field could be an Object[] or simply a String[]). You don't seem to include any proper error handling, which could potentially mess up plugins' functionality, simply due to a web site being down for maintenance or something. It's fine that readURL returns null, but in case it does, the usage code will throw an exception and terminate.

    The actual version-from-string method used could also be thrown into a utility class, and then you could use the exact same procedure when grabbing the latest version from the web, and you could just type, e.g. 1.3.2 on the page instead of having to make it separate lines.
     
  3. Offline

    oyasunadev

    Yes, I did consider doing this, as it is better Java programming. I actually do this in my own plugins, but we have so many newcomers to Java, that I wanted to keep it simple. Thanks for the heads up though! And yes, I realized i forgot the initialize.

    EDIT: I just updated the thread to use Lists. It's easier to use (no initialization,) and is better at allocating memory.
     
Thread Status:
Not open for further replies.

Share This Page