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 c198070cba1d08f9de257a5570c300d2425e5c53
Author: heliang666s <[email protected]>
AuthorDate: Tue Dec 16 11:36:06 2025 +0800

    fix: resolve resource leaks using try-with-resources
---
 dubbo-common/src/main/java/org/apache/dubbo/common/io/Bytes.java  | 8 +++++---
 .../java/org/apache/dubbo/rpc/protocol/tri/compressor/Bzip2.java  | 8 +++-----
 2 files changed, 8 insertions(+), 8 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 431350c3e9..30e6bd83a5 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
@@ -784,11 +784,13 @@ public class Bytes {
      * @throws IOException
      */
     public static byte[] zip(byte[] bytes) throws IOException {
-        try (UnsafeByteArrayOutputStream bos = new 
UnsafeByteArrayOutputStream();
-                DeflaterOutputStream os = new DeflaterOutputStream(bos)) {
+        UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream();
+        try (DeflaterOutputStream os = new DeflaterOutputStream(bos)) {
             os.write(bytes);
-            return bos.toByteArray();
+        }finally {
+            bos.close();
         }
+        return bos.toByteArray();
     }
 
     /**
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 2e9f8e0352..32971f0bf3 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
@@ -57,13 +57,13 @@ public class Bzip2 implements Compressor, DeCompressor {
             return new byte[0];
         }
 
-        try (ByteArrayOutputStream out = new ByteArrayOutputStream();
-                BZip2CompressorOutputStream cos = new 
BZip2CompressorOutputStream(out)) {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        try (BZip2CompressorOutputStream cos = new 
BZip2CompressorOutputStream(out)) {
             cos.write(payloadByteArr);
-            return out.toByteArray();
         } catch (Exception e) {
             throw new IllegalStateException(e);
         }
+        return out.toByteArray();
     }
 
     @Override
@@ -96,8 +96,6 @@ 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);
         }

Reply via email to