This is an automated email from the ASF dual-hosted git repository.
rainyu pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.3 by this push:
new 031590c4f3 [#16098] Fix resource leaks and improve code quality in
ConfigUtils (#16097)
031590c4f3 is described below
commit 031590c4f32e4fc8cd8ef1e321429beefc5a0ef0
Author: DocJlm <[email protected]>
AuthorDate: Mon Mar 2 11:27:24 2026 +0800
[#16098] Fix resource leaks and improve code quality in ConfigUtils (#16097)
* fix: fix resource leaks and improve code quality in ConfigUtils
- Replace StringBuffer with StringBuilder in replaceProperty() method
for better performance (no thread-safety needed for local variable)
- Refactor loadProperties() to use try-with-resources for FileInputStream,
eliminating manual close() calls and potential resource leaks
- Refactor multi-file loading loop to use try-with-resources for
InputStream from URL, removing empty catch blocks
- Fix resource leak in loadMigrationRule() where InputStream from URL
was never closed after reading
* revert StringBuffer change: Matcher.appendReplacement requires
StringBuffer for Java 8 compatibility
---
.../org/apache/dubbo/common/utils/ConfigUtils.java | 32 +++++++---------------
1 file changed, 10 insertions(+), 22 deletions(-)
diff --git
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java
index 8c12944ef3..796b98965e 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/ConfigUtils.java
@@ -213,13 +213,8 @@ public class ConfigUtils {
Properties properties = new Properties();
// add scene judgement in windows environment Fix 2557
if (checkFileNameExist(fileName)) {
- try {
- FileInputStream input = new FileInputStream(fileName);
- try {
- properties.load(input);
- } finally {
- input.close();
- }
+ try (FileInputStream input = new FileInputStream(fileName)) {
+ properties.load(input);
} catch (Throwable e) {
logger.warn(
COMMON_IO_EXCEPTION,
@@ -279,19 +274,11 @@ public class ConfigUtils {
logger.info("load " + fileName + " properties file from " + set);
for (java.net.URL url : set) {
- try {
- Properties p = new Properties();
- InputStream input = url.openStream();
+ try (InputStream input = url.openStream()) {
if (input != null) {
- try {
- p.load(input);
- properties.putAll(p);
- } finally {
- try {
- input.close();
- } catch (Throwable t) {
- }
- }
+ Properties p = new Properties();
+ p.load(input);
+ properties.putAll(p);
}
} catch (Throwable e) {
logger.warn(
@@ -331,9 +318,10 @@ public class ConfigUtils {
for (Set<URL> urls :
ClassLoaderResourceLoader.loadResources(fileName, classLoadersToLoad)
.values()) {
for (URL url : urls) {
- InputStream is = url.openStream();
- if (is != null) {
- return readString(is);
+ try (InputStream is = url.openStream()) {
+ if (is != null) {
+ return readString(is);
+ }
}
}
}