2 // Getdown - application installer, patcher and launcher
3 // Copyright (C) 2004-2018 Getdown authors
4 // https://github.com/threerings/getdown/blob/master/LICENSE
6 package com.threerings.getdown.util;
8 import java.io.BufferedReader;
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;
18 import com.threerings.getdown.data.SysProps;
19 import static com.threerings.getdown.Log.log;
20 import static java.nio.charset.StandardCharsets.UTF_8;
23 * Version related utilities.
25 public class VersionUtil
28 * Reads a version number from a file.
30 public static long readVersion (File vfile)
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);
39 } catch (Exception e) {
40 log.info("Unable to read version file: " + e.getMessage());
47 * Writes a version number to a file.
49 public static void writeVersion (File vfile, long version) throws IOException
51 try (PrintStream out = new PrintStream(new FileOutputStream(vfile))) {
53 } catch (Exception e) {
54 log.warning("Unable to write version file: " + e.getMessage());
59 * Parses {@code versStr} using {@code versRegex} into a (long) integer version number.
60 * @see SysProps#parseJavaVersion
62 public static long parseJavaVersion (String versRegex, String versStr)
64 Matcher m = Pattern.compile(versRegex).matcher(versStr);
65 if (!m.matches()) return 0L;
68 for (int ii = 1; ii <= m.groupCount(); ii++) {
69 String valstr = m.group(ii);
70 int value = (valstr == null) ? 0 : parseInt(valstr);
78 * Reads and parses the version from the {@code release} file bundled with a JVM.
80 public static long readReleaseVersion (File relfile, String versRegex)
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();
91 if (relvers == null) {
92 log.warning("No JAVA_VERSION line in 'release' file", "file", relfile);
95 return parseJavaVersion(versRegex, relvers);
97 } catch (Exception e) {
98 log.warning("Failed to read version from 'release' file", "file", relfile, e);
103 private static int parseInt (String str) {
105 for (int ii = 0, ll = str.length(); ii < ll; ii++) {
106 char c = str.charAt(ii);
107 if (c >= '0' && c <= '9') {