This is an automated email from the ASF dual-hosted git repository.

heliang666s pushed a commit to branch fix-resource-leaks
in repository https://gitbox.apache.org/repos/asf/dubbo.git

commit e0ebdcd5241e81453e7f490b451d242962ec290d
Author: heliang666s <[email protected]>
AuthorDate: Tue Dec 16 01:45:40 2025 +0800

    fix: resolve resource leaks using try-with-resources
---
 .../main/java/org/apache/dubbo/common/io/Bytes.java    | 18 ++++--------------
 .../java/org/apache/dubbo/common/utils/IOUtils.java    | 12 ++++++------
 .../apache/dubbo/security/cert/DubboCertManager.java   | 11 +++++------
 .../dubbo/rpc/protocol/tri/compressor/Bzip2.java       | 16 +++++++---------
 4 files changed, 22 insertions(+), 35 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java
index d3c615b755..0617e6704a 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java
@@ -22,7 +22,6 @@ import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.io.OutputStream;
 import java.nio.charset.StandardCharsets;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -786,12 +785,8 @@ public class Bytes {
      */
     public static byte[] zip(byte[] bytes) throws IOException {
         UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
-        OutputStream os = new DeflaterOutputStream(bos);
-        try {
+        try (DeflaterOutputStream os = new DeflaterOutputStream(bos)) {
             os.write(bytes);
-        } finally {
-            os.close();
-            bos.close();
         }
         return bos.toByteArray();
     }
@@ -804,16 +799,11 @@ public class Bytes {
      * @throws IOException
      */
     public static byte[] unzip(byte[] bytes) throws IOException {
-        UnsafeByteArrayInputStream bis = new UnsafeByteArrayInputStream(bytes);
-        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
-        InputStream is = new InflaterInputStream(bis);
-        try {
+        try (UnsafeByteArrayInputStream bis = new 
UnsafeByteArrayInputStream(bytes);
+                UnsafeByteArrayOutputStream bos = new 
UnsafeByteArrayOutputStream();
+                InflaterInputStream is = new InflaterInputStream(bis)) {
             IOUtils.write(is, bos);
             return bos.toByteArray();
-        } finally {
-            is.close();
-            bis.close();
-            bos.close();
         }
     }
 
diff --git 
a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java 
b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
index c2ee5587b3..364aa1a8d4 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/IOUtils.java
@@ -191,13 +191,13 @@ public class IOUtils {
 
     public static String read(InputStream is, String encoding) throws 
IOException {
         StringBuilder stringBuilder = new StringBuilder();
-        InputStreamReader inputStreamReader = new InputStreamReader(is, 
encoding);
-        char[] buf = new char[1024];
-        int len;
-        while ((len = inputStreamReader.read(buf)) != -1) {
-            stringBuilder.append(buf, 0, len);
+        try (InputStreamReader inputStreamReader = new InputStreamReader(is, 
encoding)) {
+            char[] buf = new char[1024];
+            int len;
+            while ((len = inputStreamReader.read(buf)) != -1) {
+                stringBuilder.append(buf, 0, len);
+            }
         }
-        inputStreamReader.close();
         return stringBuilder.toString();
     }
 
diff --git 
a/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java
 
b/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java
index bddef4a094..8bae66574a 100644
--- 
a/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java
+++ 
b/dubbo-plugin/dubbo-security/src/main/java/org/apache/dubbo/security/cert/DubboCertManager.java
@@ -367,12 +367,11 @@ public class DubboCertManager {
      */
     private String generatePemKey(String type, byte[] content) throws 
IOException {
         PemObject pemObject = new PemObject(type, content);
-        StringWriter str = new StringWriter();
-        JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str);
-        jcaPEMWriter.writeObject(pemObject);
-        jcaPEMWriter.close();
-        str.close();
-        return str.toString();
+        try (StringWriter str = new StringWriter();
+                JcaPEMWriter jcaPEMWriter = new JcaPEMWriter(str)) {
+            jcaPEMWriter.writeObject(pemObject);
+            return str.toString();
+        }
     }
 
     /**
diff --git 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java
 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java
index c76ea1459a..e77fccc120 100644
--- 
a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java
+++ 
b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java
@@ -58,11 +58,8 @@ public class Bzip2 implements Compressor, DeCompressor {
         }
 
         ByteArrayOutputStream out = new ByteArrayOutputStream();
-        BZip2CompressorOutputStream cos;
-        try {
-            cos = new BZip2CompressorOutputStream(out);
+        try (BZip2CompressorOutputStream cos = new 
BZip2CompressorOutputStream(out)) {
             cos.write(payloadByteArr);
-            cos.close();
         } catch (Exception e) {
             throw new IllegalStateException(e);
         }
@@ -85,11 +82,10 @@ public class Bzip2 implements Compressor, DeCompressor {
             return new byte[0];
         }
 
-        ByteArrayOutputStream out = new ByteArrayOutputStream();
-        ByteArrayInputStream in = new ByteArrayInputStream(payloadByteArr);
-        try {
+        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
+                ByteArrayInputStream in = new 
ByteArrayInputStream(payloadByteArr);
+                BZip2CompressorInputStream unZip = new 
BZip2CompressorInputStream(in)) {
             int totalBytesRead = 0;
-            BZip2CompressorInputStream unZip = new 
BZip2CompressorInputStream(in);
             byte[] buffer = new byte[2048];
             int n;
             while ((n = unZip.read(buffer)) >= 0) {
@@ -100,9 +96,11 @@ public class Bzip2 implements Compressor, DeCompressor {
                 }
                 out.write(buffer, 0, n);
             }
+            return out.toByteArray();
+        } catch (RpcException e) {
+            throw e;
         } catch (Exception e) {
             throw new IllegalStateException(e);
         }
-        return out.toByteArray();
     }
 }

Reply via email to