VersionUtil.java 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package com.mes.update;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.InputStreamReader;
  6. import java.nio.charset.StandardCharsets;
  7. /**
  8. * 本地/远程版本读写与三段数字比较。
  9. */
  10. public final class VersionUtil {
  11. private VersionUtil() {
  12. }
  13. /**
  14. * 读取本地版本:优先安装目录旁 app.version,否则使用配置中的默认版本。
  15. */
  16. public static String resolveLocalVersion(File installDir, String configVersion) {
  17. if (installDir != null) {
  18. File versionFile = new File(installDir, "app.version");
  19. String fromFile = readVersionFile(versionFile);
  20. if (fromFile != null && !fromFile.isEmpty()) {
  21. return fromFile;
  22. }
  23. }
  24. if (configVersion != null && !configVersion.trim().isEmpty()) {
  25. return configVersion.trim();
  26. }
  27. return "0.0.0";
  28. }
  29. public static String readVersionFile(File versionFile) {
  30. if (versionFile == null || !versionFile.isFile()) {
  31. return null;
  32. }
  33. try (BufferedReader reader = new BufferedReader(
  34. new InputStreamReader(new FileInputStream(versionFile), StandardCharsets.UTF_8))) {
  35. String line = reader.readLine();
  36. if (line == null) {
  37. return null;
  38. }
  39. return line.trim();
  40. } catch (Exception e) {
  41. return null;
  42. }
  43. }
  44. /**
  45. * 比较两个版本号。返回 >0 表示 a 更新,<0 表示 b 更新,0 表示相等。
  46. */
  47. public static int compare(String a, String b) {
  48. int[] pa = parse(a);
  49. int[] pb = parse(b);
  50. int len = Math.max(pa.length, pb.length);
  51. for (int i = 0; i < len; i++) {
  52. int va = i < pa.length ? pa[i] : 0;
  53. int vb = i < pb.length ? pb[i] : 0;
  54. if (va != vb) {
  55. return va - vb;
  56. }
  57. }
  58. return 0;
  59. }
  60. public static boolean isNewer(String remote, String local) {
  61. return compare(remote, local) > 0;
  62. }
  63. private static int[] parse(String version) {
  64. if (version == null || version.trim().isEmpty()) {
  65. return new int[]{0, 0, 0};
  66. }
  67. String cleaned = version.trim();
  68. if (cleaned.startsWith("v") || cleaned.startsWith("V")) {
  69. cleaned = cleaned.substring(1);
  70. }
  71. String[] parts = cleaned.split("[^0-9]+");
  72. int[] result = new int[Math.max(3, parts.length)];
  73. int idx = 0;
  74. for (String part : parts) {
  75. if (part == null || part.isEmpty()) {
  76. continue;
  77. }
  78. try {
  79. result[idx++] = Integer.parseInt(part);
  80. } catch (NumberFormatException ignored) {
  81. result[idx++] = 0;
  82. }
  83. }
  84. if (idx == 0) {
  85. return new int[]{0, 0, 0};
  86. }
  87. if (idx < 3) {
  88. int[] padded = new int[3];
  89. System.arraycopy(result, 0, padded, 0, idx);
  90. return padded;
  91. }
  92. int[] exact = new int[idx];
  93. System.arraycopy(result, 0, exact, 0, idx);
  94. return exact;
  95. }
  96. }