From 9bbf15fa74813b575d48cfdbb07787f56e918ed1 Mon Sep 17 00:00:00 2001 From: federico Date: Tue, 4 Aug 2026 17:08:02 +0800 Subject: [PATCH] fix(config): close resource streams --- .../org/tron/core/config/Configuration.java | 3 +- .../java/org/tron/core/config/args/Args.java | 11 +++---- .../org/tron/core/zen/ZksnarkInitService.java | 9 ++++-- .../org/tron/common/utils/FileUtilTest.java | 28 ++++++++---------- .../services/http/BroadcastServletTest.java | 16 +++++----- ...GetTransactionByIdSolidityServletTest.java | 29 +++++++++---------- .../org/tron/plugins/utils/FileUtils.java | 13 ++++++--- 7 files changed, 54 insertions(+), 55 deletions(-) diff --git a/common/src/main/java/org/tron/core/config/Configuration.java b/common/src/main/java/org/tron/core/config/Configuration.java index 80735290b8c..a36c9825470 100644 --- a/common/src/main/java/org/tron/core/config/Configuration.java +++ b/common/src/main/java/org/tron/core/config/Configuration.java @@ -50,7 +50,7 @@ private static void resolveConfigFile(String fileName, File confFile) { if (confFile.exists()) { config = ConfigFactory.parseFile(confFile) .withFallback(ConfigFactory.defaultReference()); - } else if (Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) + } else if (Thread.currentThread().getContextClassLoader().getResource(fileName) != null) { config = ConfigFactory.load(fileName); } else { @@ -59,4 +59,3 @@ private static void resolveConfigFile(String fileName, File confFile) { } } } - diff --git a/framework/src/main/java/org/tron/core/config/args/Args.java b/framework/src/main/java/org/tron/core/config/args/Args.java index 0bca242606e..a48452f2de9 100644 --- a/framework/src/main/java/org/tron/core/config/args/Args.java +++ b/framework/src/main/java/org/tron/core/config/args/Args.java @@ -1186,9 +1186,8 @@ public String getOutputDirectory() { private static void printVersion() { Properties properties = new Properties(); boolean noGitProperties = true; - try { - InputStream in = Thread.currentThread() - .getContextClassLoader().getResourceAsStream("git.properties"); + try (InputStream in = Thread.currentThread() + .getContextClassLoader().getResourceAsStream("git.properties")) { if (in != null) { noGitProperties = false; properties.load(in); @@ -1276,9 +1275,8 @@ public static String upperFirst(String name) { private static String getCommitIdAbbrev() { Properties properties = new Properties(); - try { - InputStream in = Thread.currentThread() - .getContextClassLoader().getResourceAsStream("git.properties"); + try (InputStream in = Thread.currentThread() + .getContextClassLoader().getResourceAsStream("git.properties")) { if (in == null) { logger.warn("git.properties not found on classpath"); return ""; @@ -1315,4 +1313,3 @@ private static Map getOptionGroup() { return optionGroupMap; } } - diff --git a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java index dfc4b428836..e0748e99f3b 100644 --- a/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java +++ b/framework/src/main/java/org/tron/core/zen/ZksnarkInitService.java @@ -57,11 +57,14 @@ public static void librustzcashInitZksnarkParams() { } private static String getParamsFile(String fileName) { - InputStream in = Thread.currentThread().getContextClassLoader() - .getResourceAsStream("params" + File.separator + fileName); + String resourcePath = "params" + File.separator + fileName; File fileOut = new File(System.getProperty("java.io.tmpdir") + File.separator + fileName + "." + System.currentTimeMillis()); - try { + try (InputStream in = Thread.currentThread().getContextClassLoader() + .getResourceAsStream(resourcePath)) { + if (in == null) { + throw new IllegalStateException("Resource not found: " + resourcePath); + } FileUtils.copyToFile(in, fileOut); } catch (IOException e) { logger.error(e.getMessage(), e); diff --git a/framework/src/test/java/org/tron/common/utils/FileUtilTest.java b/framework/src/test/java/org/tron/common/utils/FileUtilTest.java index c22e83760a1..b062c8c394e 100644 --- a/framework/src/test/java/org/tron/common/utils/FileUtilTest.java +++ b/framework/src/test/java/org/tron/common/utils/FileUtilTest.java @@ -8,18 +8,13 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.FileVisitResult; -import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.Paths; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.ArrayList; import java.util.Arrays; import java.util.Comparator; import java.util.List; +import java.util.stream.Stream; import org.junit.After; -import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -39,15 +34,16 @@ public void setUp() throws IOException { @After public void tearDown() throws IOException { - Files.walk(tempDir) - .sorted(Comparator.reverseOrder()) - .forEach(path -> { - try { - Files.delete(path); - } catch (IOException e) { - e.printStackTrace(); - } - }); + try (Stream paths = Files.walk(tempDir)) { + paths.sorted(Comparator.reverseOrder()) + .forEach(path -> { + try { + Files.delete(path); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } } @Test @@ -126,4 +122,4 @@ public void testCreateDirIfNotExists() { } -} \ No newline at end of file +} diff --git a/framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java b/framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java index d6bf3850f30..024467eef1b 100644 --- a/framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/BroadcastServletTest.java @@ -146,17 +146,17 @@ public void doPostTest() throws IOException { } Assert.assertNotNull(result); in.close(); - writer.flush(); - FileInputStream fileInputStream = new FileInputStream("temp.txt"); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + writer.close(); StringBuilder sb = new StringBuilder(); - String text; - while ((text = bufferedReader.readLine()) != null) { - sb.append(text); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( + new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) { + String text; + while ((text = bufferedReader.readLine()) != null) { + sb.append(text); + } } Assert.assertTrue(sb.toString().contains("null")); httpUrlConnection.disconnect(); } -} \ No newline at end of file +} diff --git a/framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java b/framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java index e1abb41d1e1..d6600010ef8 100644 --- a/framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java +++ b/framework/src/test/java/org/tron/core/services/http/solidity/GetTransactionByIdSolidityServletTest.java @@ -129,15 +129,15 @@ public void doPostTest() throws IOException { } Assert.assertNotNull(result); in.close(); - writer.flush(); - FileInputStream fileInputStream = new FileInputStream("temp.txt"); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + writer.close(); StringBuilder sb = new StringBuilder(); - String text; - while ((text = bufferedReader.readLine()) != null) { - sb.append(text); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( + new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) { + String text; + while ((text = bufferedReader.readLine()) != null) { + sb.append(text); + } } Assert.assertTrue(sb.toString().contains("null")); httpUrlConnection.disconnect(); @@ -185,18 +185,17 @@ public void doGetTest() throws IOException { } Assert.assertNotNull(result); in.close(); - writer.flush(); - FileInputStream fileInputStream = new FileInputStream("temp.txt"); - InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + writer.close(); StringBuilder sb = new StringBuilder(); - String text; - while ((text = bufferedReader.readLine()) != null) { - sb.append(text); + try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( + new FileInputStream("temp.txt"), StandardCharsets.UTF_8))) { + String text; + while ((text = bufferedReader.readLine()) != null) { + sb.append(text); + } } Assert.assertTrue(sb.toString().contains("null")); httpUrlConnection.disconnect(); } } - diff --git a/plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java b/plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java index b07b4469dc3..04b2efa476a 100644 --- a/plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java +++ b/plugins/src/main/java/common/org/tron/plugins/utils/FileUtils.java @@ -20,6 +20,7 @@ import java.nio.file.StandardCopyOption; import java.util.List; import java.util.Properties; +import java.util.stream.Stream; import lombok.extern.slf4j.Slf4j; @Slf4j @@ -141,8 +142,10 @@ public static void copyDatabases(Path src, Path dest, List subDirs) subDirs.forEach(dir -> { if (isExists(Paths.get(src.toString(), dir).toString())) { try { - Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) - .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); + try (Stream paths = Files.walk( + Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) { + paths.forEach(source -> copy(source, dest.resolve(src.relativize(source)))); + } } catch (IOException e) { logger.error("copy database failed, src: {}, dest: {}, error: {}", Paths.get(src.toString(), dir), Paths.get(dest.toString(), dir), e.getMessage()); @@ -156,8 +159,10 @@ public static void copyDir(Path src, Path dest, String dir) { if (isExists(Paths.get(src.toString(), dir).toString())) { try { if (createDirIfNotExists(Paths.get(dest.toString(), dir).toString())) { - Files.walk(Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS) - .forEach(source -> copy(source, dest.resolve(src.relativize(source)))); + try (Stream paths = Files.walk( + Paths.get(src.toString(), dir), FileVisitOption.FOLLOW_LINKS)) { + paths.forEach(source -> copy(source, dest.resolve(src.relativize(source)))); + } } else { throw new IOException(String.format("dest %s create fail ", Paths.get(dest.toString(), dir)));