From b0add98489125e8455a0e62a2a457005d547e101 Mon Sep 17 00:00:00 2001 From: Hiroki Tokunaga Date: Thu, 28 May 2026 21:41:09 +0900 Subject: [PATCH] refactor: extract `AppVersion` for `/version` resource --- .../java/core/packetproxy/PacketProxy.java | 9 +--- .../core/packetproxy/common/AppVersion.java | 50 +++++++++++++++++++ 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 src/main/java/core/packetproxy/common/AppVersion.java diff --git a/src/main/java/core/packetproxy/PacketProxy.java b/src/main/java/core/packetproxy/PacketProxy.java index d9b2ca61..31f61e0c 100644 --- a/src/main/java/core/packetproxy/PacketProxy.java +++ b/src/main/java/core/packetproxy/PacketProxy.java @@ -16,10 +16,9 @@ package packetproxy; import java.io.File; -import java.io.InputStream; import java.sql.SQLException; import javax.swing.*; -import org.apache.commons.io.IOUtils; +import packetproxy.common.AppVersion; import packetproxy.common.I18nString; import packetproxy.common.Utils; import packetproxy.gui.GUIMain; @@ -119,11 +118,7 @@ public void start() throws Exception { } private void startGUI() throws Exception { - String version = "1.0.0"; - InputStream versionStream = getClass().getResourceAsStream("/version"); - if (versionStream != null) { - version = IOUtils.toString(versionStream); - } + var version = AppVersion.get(); gui = GUIMain.getInstance(String.format("PacketProxy %s", version)); gui.setVisible(true); } diff --git a/src/main/java/core/packetproxy/common/AppVersion.java b/src/main/java/core/packetproxy/common/AppVersion.java new file mode 100644 index 00000000..39d095e7 --- /dev/null +++ b/src/main/java/core/packetproxy/common/AppVersion.java @@ -0,0 +1,50 @@ +/* + * Copyright 2026 DeNA Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package packetproxy.common; + +import java.io.IOException; +import java.io.InputStream; +import org.apache.commons.io.IOUtils; + +public final class AppVersion { + + private static final String DEFAULT_VERSION = "1.0.0"; + private static String cached; + + private AppVersion() { + } + + public static String get() { + if (cached == null) { + + cached = load(); + } + return cached; + } + + private static String load() { + try (InputStream in = AppVersion.class.getResourceAsStream("/version")) { + if (in == null) { + + return DEFAULT_VERSION; + } + return IOUtils.toString(in).trim(); + } catch (IOException e) { + + return DEFAULT_VERSION; + } + } +}