JAL-3130 adapted getdown src. attempt 2. first attempt failed due to cp'ed .git files
[jalview.git] / getdown / src / getdown / core / src / main / java / com / threerings / getdown / util / VersionUtil.java
1 //
2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
5
6 package com.threerings.getdown.util;
7
8 import java.io.BufferedReader;
9 import java.io.File;
10 import java.io.FileInputStream;
11 import java.io.FileOutputStream;
12 import java.io.IOException;
13 import java.io.InputStreamReader;
14 import java.io.PrintStream;
15 import java.util.regex.Matcher;
16 import java.util.regex.Pattern;
17
18 import com.threerings.getdown.data.SysProps;
19 import static com.threerings.getdown.Log.log;
20 import static java.nio.charset.StandardCharsets.UTF_8;
21
22 /**
23  * Version related utilities.
24  */
25 public class VersionUtil
26 {
27     /**
28      * Reads a version number from a file.
29      */
30     public static long readVersion (File vfile)
31     {
32         long fileVersion = -1;
33         try (BufferedReader bin =
34              new BufferedReader(new InputStreamReader(new FileInputStream(vfile), UTF_8))) {
35             String vstr = bin.readLine();
36             if (!StringUtil.isBlank(vstr)) {
37                 fileVersion = Long.parseLong(vstr);
38             }
39         } catch (Exception e) {
40             log.info("Unable to read version file: " + e.getMessage());
41         }
42
43         return fileVersion;
44     }
45
46     /**
47      * Writes a version number to a file.
48      */
49     public static void writeVersion (File vfile, long version) throws IOException
50     {
51         try (PrintStream out = new PrintStream(new FileOutputStream(vfile))) {
52             out.println(version);
53         } catch (Exception e) {
54             log.warning("Unable to write version file: " + e.getMessage());
55         }
56     }
57
58     /**
59      * Parses {@code versStr} using {@code versRegex} into a (long) integer version number.
60      * @see SysProps#parseJavaVersion
61      */
62     public static long parseJavaVersion (String versRegex, String versStr)
63     {
64         Matcher m = Pattern.compile(versRegex).matcher(versStr);
65         if (!m.matches()) return 0L;
66
67         long vers = 0L;
68         for (int ii = 1; ii <= m.groupCount(); ii++) {
69             String valstr = m.group(ii);
70             int value = (valstr == null) ? 0 : parseInt(valstr);
71             vers *= 100;
72             vers += value;
73         }
74         return vers;
75     }
76
77     /**
78      * Reads and parses the version from the {@code release} file bundled with a JVM.
79      */
80     public static long readReleaseVersion (File relfile, String versRegex)
81     {
82         try (BufferedReader in =
83              new BufferedReader(new InputStreamReader(new FileInputStream(relfile), UTF_8))) {
84             String line = null, relvers = null;
85             while ((line = in.readLine()) != null) {
86                 if (line.startsWith("JAVA_VERSION=")) {
87                     relvers = line.substring("JAVA_VERSION=".length()).replace('"', ' ').trim();
88                 }
89             }
90
91             if (relvers == null) {
92                 log.warning("No JAVA_VERSION line in 'release' file", "file", relfile);
93                 return 0L;
94             }
95             return parseJavaVersion(versRegex, relvers);
96
97         } catch (Exception e) {
98             log.warning("Failed to read version from 'release' file", "file", relfile, e);
99             return 0L;
100         }
101     }
102
103     private static int parseInt (String str) {
104         int value = 0;
105         for (int ii = 0, ll = str.length(); ii < ll; ii++) {
106             char c = str.charAt(ii);
107             if (c >= '0' && c <= '9') {
108                 value *= 10;
109                 value += (c - '0');
110             }
111         }
112         return value;
113     }
114 }