From 702c2986073a00d03ec53a19a0fff4123290d478 Mon Sep 17 00:00:00 2001 From: Senrian <47714364+Senrian@users.noreply.github.com> Date: Sun, 29 Mar 2026 18:17:39 +0800 Subject: [PATCH] fix: use try-with-resources in copyFile to prevent resource leak (issue #10208) --- .../rocketmq/common/utils/IOTinyUtils.java | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java index acba540d3a5..d019f729f5f 100644 --- a/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java +++ b/common/src/main/java/org/apache/rocketmq/common/utils/IOTinyUtils.java @@ -86,19 +86,11 @@ static public void copyFile(String source, String target) throws IOException { throw new RuntimeException("failed to create target file."); } - FileChannel sc = null; - FileChannel tc = null; - try { - tc = new FileOutputStream(tf).getChannel(); - sc = new FileInputStream(sf).getChannel(); + try (FileInputStream fis = new FileInputStream(sf); + FileOutputStream fos = new FileOutputStream(tf); + FileChannel sc = fis.getChannel(); + FileChannel tc = fos.getChannel()) { sc.transferTo(0, sc.size(), tc); - } finally { - if (null != sc) { - sc.close(); - } - if (null != tc) { - tc.close(); - } } }