From 0dc2ffdbe213643449cce6851738aa37668e6fef Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 16 Jul 2024 15:08:21 +0800 Subject: [PATCH 1/3] java close in finally block Signed-off-by: huruitao --- .../src/com/sk/gn/dialog/GenDialogPane.java | 74 +++++++++++---- .../src/com/sk/gn/utils/FileUtil.java | 7 +- .../src/com/sk/dialog/GenerateDialogPane.java | 77 ++++++++++++---- .../com/sk/ts/dialog/GenerateDialogPane.java | 77 ++++++++++++---- .../src/com/sk/na/ng/GenDTS.java | 75 ++++++++++++---- .../dialog/ServiceGenerateDialogPane.java | 77 ++++++++++++---- .../com/kh/scan/dialog/ApiScanDialogPane.java | 90 +++++++++++++++---- 7 files changed, 374 insertions(+), 103 deletions(-) diff --git a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java index ddf1bd5c..849506c0 100644 --- a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java +++ b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java @@ -422,20 +422,39 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String sErr; - String sOut; - sErr = getErrorResult(stdError); - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // Handle exception + LOG.error(e); + } finally { + // Close resources in finally block to ensure they are closed even if an exception occurs + try { + stdInput.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error(e); } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -505,15 +524,29 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP @Override public void run() { + InputStreamReader isr = null; + BufferedReader br = null; try { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader br和InputStreamReader isr被关闭 + try { + br.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + isr.close(); + } catch (IOException e) { + LOG.error(e); + } } } } @@ -532,14 +565,23 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP @Override public void run() { - BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); - genResultLog(process); + BufferedReader br = null; try { + br = new BufferedReader(new InputStreamReader(process.getInputStream())); + genResultLog(process); while (br.readLine() != null) { LOG.info(" callExtProcess "); } } catch (IOException ioException) { LOG.error(" callExtProcess error" + ioException); + } finally { + // 确保BufferedReader br被关闭 + try { + br.close(); + } catch (IOException e) { + // 处理关闭BufferedReader时的异常 + LOG.error(e); + } } } } diff --git a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java index 8f771c19..3d52dca0 100644 --- a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java +++ b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java @@ -164,11 +164,16 @@ public class FileUtil { } FileOutputStream fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); - fw.close(); } catch (IOException e) { GenNotification.notifyMessage(project, e.getMessage(), "Can not Find File:" + oldPath, NotificationType.ERROR); LOG.error(e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing FileOutputStream", e); + } } } } diff --git a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java index d3c781bf..6cbed64b 100644 --- a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java +++ b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java @@ -574,9 +574,19 @@ public class GenerateDialogPane extends JDialog { LOG.info("writeTmpFile createNewFile error"); } } - FileOutputStream fw = new FileOutputStream(file); - fw.write(bs, 0, bs.length); - fw.close(); + try { + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + } } /** @@ -585,20 +595,39 @@ public class GenerateDialogPane extends JDialog { * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String sErr; - String sOut; - sErr = getErrorResult(stdError); - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + // 确保BufferedReader对象被关闭 + try { + stdInput.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error("Error closing stdError", e); } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -660,15 +689,29 @@ public class GenerateDialogPane extends JDialog { @Override public void run() { + InputStreamReader isr = null; + BufferedReader br = null; try { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader和InputStreamReader被关闭 + try { + br.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + isr.close(); + } catch (IOException e) { + LOG.error(e); + } } } } diff --git a/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java b/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java index 676c9d67..7d1d10f3 100644 --- a/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java +++ b/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java @@ -320,9 +320,19 @@ public class GenerateDialogPane extends JDialog { LOG.info("writeTmpFile createNewFile error"); } } - FileOutputStream fw = new FileOutputStream(file); - fw.write(bs, 0, bs.length); - fw.close(); + try { + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + } } /** @@ -331,20 +341,39 @@ public class GenerateDialogPane extends JDialog { * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String sErr; - String sOut; - sErr = getErrorResult(stdError); - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error in genResultLog", e); + } finally { + // 确保BufferedReader对象被关闭 + try { + stdInput.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); // 记录关闭stdInput时的错误 + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error("Error closing stdError", e); // 记录关闭stdError时的错误 } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -406,15 +435,29 @@ public class GenerateDialogPane extends JDialog { @Override public void run() { + InputStreamReader inputStreamReader = null; + BufferedReader bufferedReader = null; try { - InputStreamReader inputStreamReader = new InputStreamReader(is); - BufferedReader bufferedReader = new BufferedReader(inputStreamReader); + inputStreamReader = new InputStreamReader(is); + bufferedReader = new BufferedReader(inputStreamReader); String readLine; while ((readLine = bufferedReader.readLine()) != null) { LOG.error("StreamConsumer" + readLine); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader和InputStreamReader被关闭 + try { + bufferedReader.close(); + } catch (IOException e) { + LOG.error("Error closing BufferedReader", e); + } + try { + inputStreamReader.close(); + } catch (IOException e) { + LOG.error("Error closing inputStreamReader", e); + } } } } diff --git a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java index 52384d93..f6bfadc5 100644 --- a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java +++ b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java @@ -92,21 +92,40 @@ public class GenDts extends AnAction { * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getErrorStream(), + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8)); - String sErr = getErrorResult(stdError); - String sOut; - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + String sErr = getErrorResult(stdError); + String sOut; + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // Handle exception + LOG.error(e); + } finally { + // Close resources in finally block to ensure they are closed even if an exception occurs + try { + stdInput.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error(e); } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -177,15 +196,29 @@ public class GenDts extends AnAction { @Override public void run() { + InputStreamReader isr = null; + BufferedReader br = null; try { - InputStreamReader isr = new InputStreamReader(is, StandardCharsets.UTF_8); - BufferedReader br = new BufferedReader(isr); + isr = new InputStreamReader(is, StandardCharsets.UTF_8); + br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader br和InputStreamReader isr被关闭 + try { + br.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + isr.close(); + } catch (IOException e) { + LOG.error(e); + } } } } @@ -240,9 +273,19 @@ public class GenDts extends AnAction { LOG.info("writeTmpFile createNewFile error"); } } - FileOutputStream fw = new FileOutputStream(file); - fw.write(bs, 0, bs.length); - fw.close(); + try { + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + } } /** diff --git a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java index 39b0931c..3065f435 100644 --- a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java +++ b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java @@ -284,9 +284,19 @@ public class ServiceGenerateDialogPane extends JDialog { LOG.info("writeTmpFile createNewFile error"); } } - FileOutputStream fw = new FileOutputStream(file); - fw.write(bs, 0, bs.length); - fw.close(); + try { + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + } } /** @@ -295,20 +305,39 @@ public class ServiceGenerateDialogPane extends JDialog { * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String sErr; - String sOut; - sErr = getErrorResult(stdError); - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // Handle exception + LOG.error(e); + } finally { + // Close resources in finally block to ensure they are closed even if an exception occurs + try { + stdInput.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error(e); } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -370,15 +399,29 @@ public class ServiceGenerateDialogPane extends JDialog { @Override public void run() { + InputStreamReader isr = null; + BufferedReader br = null; try { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader br和InputStreamReader isr被关闭 + try { + br.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + isr.close(); + } catch (IOException e) { + LOG.error(e); + } } } } diff --git a/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java b/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java index 796f0323..5e99f3e1 100644 --- a/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java +++ b/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java @@ -278,9 +278,19 @@ public class ApiScanDialogPane extends JDialog { LOG.info("writeTmpFile createNewFile error"); } } - FileOutputStream fw = new FileOutputStream(file); - fw.write(bs, 0, bs.length); - fw.close(); + try { + FileOutputStream fw = new FileOutputStream(file); + fw.write(bs, 0, bs.length); + } catch (IOException e) { + // 处理可能发生的IOException + LOG.error("Error reading from process streams", e); + } finally { + try { + fw.close(); + } catch (IOException e) { + LOG.error("Error closing stdInput", e); + } + } } /** @@ -289,20 +299,39 @@ public class ApiScanDialogPane extends JDialog { * @param process 进程ID */ private void genResultLog(Process process) { - BufferedReader stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); - String sErr; - String sOut; - sErr = getErrorResult(stdError); - if (TextUtils.isEmpty(sErr)) { - sOut = genInputLog(stdInput); - if (!generateIsSuccess(sOut)) { - sErrorMessage = sOut; + BufferedReader stdInput = null; + BufferedReader stdError = null; + try { + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + String sErr; + String sOut; + sErr = getErrorResult(stdError); + if (TextUtils.isEmpty(sErr)) { + sOut = genInputLog(stdInput); + if (!generateIsSuccess(sOut)) { + sErrorMessage = sOut; + } + } else { + generateSuccess = false; + sErrorMessage = sErr; + } + } catch (IOException e) { + // Handle exception + LOG.error(e); + } finally { + // Close resources in finally block to ensure they are closed even if an exception occurs + try { + stdInput.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + stdError.close(); + } catch (IOException e) { + LOG.error(e); } - return; } - generateSuccess = false; - sErrorMessage = sErr; } /** @@ -364,15 +393,29 @@ public class ApiScanDialogPane extends JDialog { @Override public void run() { + InputStreamReader isr = null; + BufferedReader br = null; try { - InputStreamReader isr = new InputStreamReader(is); - BufferedReader br = new BufferedReader(isr); + isr = new InputStreamReader(is); + br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { LOG.info(line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); + } finally { + // 确保BufferedReader br和InputStreamReader isr被关闭 + try { + br.close(); + } catch (IOException e) { + LOG.error(e); + } + try { + isr.close(); + } catch (IOException e) { + LOG.error(e); + } } } } @@ -391,14 +434,23 @@ public class ApiScanDialogPane extends JDialog { @Override public void run() { - BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream())); - genResultLog(process); + BufferedReader br = null; try { + br = new BufferedReader(new InputStreamReader(process.getInputStream())); + genResultLog(process); while (br.readLine() != null) { LOG.info(" callExtProcess "); } } catch (IOException ioException) { LOG.error(" callExtProcess error" + ioException); + } finally { + // 确保BufferedReader br被关闭 + try { + br.close(); + } catch (IOException e) { + // 处理关闭BufferedReader时的异常 + LOG.error(e); + } } } } -- Gitee From efda716fde9f7c5d3cea554d6198ed759dbe1dd2 Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 16 Jul 2024 16:28:42 +0800 Subject: [PATCH 2/3] codecheck Signed-off-by: huruitao --- .../src/com/sk/gn/dialog/GenDialogPane.java | 11 +++--- .../src/com/sk/gn/utils/FileUtil.java | 3 +- .../src/com/sk/dialog/GenerateDialogPane.java | 11 +++--- .../com/sk/ts/dialog/GenerateDialogPane.java | 13 ++++--- .../src/com/sk/na/ng/GenDTS.java | 3 +- .../dialog/ServiceGenerateDialogPane.java | 11 +++--- .../com/kh/scan/dialog/ApiScanDialogPane.java | 36 ++++++++++--------- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java index a10f4bfd..b3ce2983 100644 --- a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java +++ b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java @@ -425,8 +425,10 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP BufferedReader stdInput = null; BufferedReader stdError = null; try { - stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + StandardCharsets.UTF_8)); String sErr = ''; String sOut; sErr = getErrorResult(stdError); @@ -527,7 +529,7 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP InputStreamReader isr = null; BufferedReader br = null; try { - isr = new InputStreamReader(is); + isr = new InputStreamReader(is, StandardCharsets.UTF_8); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { @@ -567,7 +569,8 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP public void run() { BufferedReader br = null; try { - br = new BufferedReader(new InputStreamReader(process.getInputStream())); + br = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); genResultLog(process); while (br.readLine() != null) { LOG.info(" callExtProcess "); diff --git a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java index 4f1ed6d1..17165ed9 100644 --- a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java +++ b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/utils/FileUtil.java @@ -152,6 +152,7 @@ public class FileUtil { */ public static void writeTmpFile(String path, String oldPath, Project project) { File file = new File(path); + FileOutputStream fw = null ; try (InputStream inputStream = FileUtil.class.getClassLoader().getResourceAsStream(oldPath)) { if (inputStream == null) { throw new IOException("exec File InputStream is Null"); @@ -162,7 +163,7 @@ public class FileUtil { if (!isNewFile) { LOG.info("writeTmpFile createNewFile error"); } - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { GenNotification.notifyMessage(project, e.getMessage(), "Can not Find File:" + oldPath, diff --git a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java index 6cbed64b..b6e32bd1 100644 --- a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java +++ b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java @@ -568,6 +568,7 @@ public class GenerateDialogPane extends JDialog { */ private void writeTmpFile(String path, byte[] bs) throws IOException { File file = new File(path); + FileOutputStream fw = null; if (!file.exists()) { boolean isNewFile = file.createNewFile(); if (!isNewFile) { @@ -575,7 +576,7 @@ public class GenerateDialogPane extends JDialog { } } try { - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { // 处理可能发生的IOException @@ -598,8 +599,10 @@ public class GenerateDialogPane extends JDialog { BufferedReader stdInput = null; BufferedReader stdError = null; try { - stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + StandardCharsets.UTF_8)); String sErr; String sOut; sErr = getErrorResult(stdError); @@ -692,7 +695,7 @@ public class GenerateDialogPane extends JDialog { InputStreamReader isr = null; BufferedReader br = null; try { - isr = new InputStreamReader(is); + isr = new InputStreamReader(is, StandardCharsets.UTF_8); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { diff --git a/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java b/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java index 7d1d10f3..af527b76 100644 --- a/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java +++ b/src/intellij_plugin/h2dts/ts_IntelliJ_plugin/src/com/sk/ts/dialog/GenerateDialogPane.java @@ -314,6 +314,7 @@ public class GenerateDialogPane extends JDialog { */ private void writeTmpFile(String path, byte[] bs) throws IOException { File file = new File(path); + FileOutputStream fw = null; if (!file.exists()) { boolean isNewFile = file.createNewFile(); if (!isNewFile) { @@ -321,7 +322,7 @@ public class GenerateDialogPane extends JDialog { } } try { - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { // 处理可能发生的IOException @@ -344,8 +345,10 @@ public class GenerateDialogPane extends JDialog { BufferedReader stdInput = null; BufferedReader stdError = null; try { - stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + StandardCharsets.UTF_8)); String sErr; String sOut; sErr = getErrorResult(stdError); @@ -438,7 +441,7 @@ public class GenerateDialogPane extends JDialog { InputStreamReader inputStreamReader = null; BufferedReader bufferedReader = null; try { - inputStreamReader = new InputStreamReader(is); + inputStreamReader = new InputStreamReader(is, StandardCharsets.UTF_8); bufferedReader = new BufferedReader(inputStreamReader); String readLine; while ((readLine = bufferedReader.readLine()) != null) { @@ -447,7 +450,7 @@ public class GenerateDialogPane extends JDialog { } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); } finally { - // 确保BufferedReader和InputStreamReader被关闭 + // 确保BufferedReader和InputStreamReader被关闭 try { bufferedReader.close(); } catch (IOException e) { diff --git a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java index f6bfadc5..cf1dd1cd 100644 --- a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java +++ b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java @@ -267,6 +267,7 @@ public class GenDts extends AnAction { */ private void writeTmpFile(String path, byte[] bs) throws IOException { File file = new File(path); + FileOutputStream fw = null; if (!file.exists()) { boolean isNewFile = file.createNewFile(); if (!isNewFile) { @@ -274,7 +275,7 @@ public class GenDts extends AnAction { } } try { - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { // 处理可能发生的IOException diff --git a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java index 3065f435..a2f586c6 100644 --- a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java +++ b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java @@ -278,6 +278,7 @@ public class ServiceGenerateDialogPane extends JDialog { */ private void writeTmpFile(String path, byte[] bs) throws IOException { File file = new File(path); + FileOutputStream fw = null; if (!file.exists()) { boolean isNewFile = file.createNewFile(); if (!isNewFile) { @@ -285,7 +286,7 @@ public class ServiceGenerateDialogPane extends JDialog { } } try { - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { // 处理可能发生的IOException @@ -308,8 +309,10 @@ public class ServiceGenerateDialogPane extends JDialog { BufferedReader stdInput = null; BufferedReader stdError = null; try { - stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + StandardCharsets.UTF_8)); String sErr; String sOut; sErr = getErrorResult(stdError); @@ -402,7 +405,7 @@ public class ServiceGenerateDialogPane extends JDialog { InputStreamReader isr = null; BufferedReader br = null; try { - isr = new InputStreamReader(is); + isr = new InputStreamReader(is, StandardCharsets.UTF_8); br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null) { diff --git a/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java b/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java index 5e99f3e1..a6fe6a04 100644 --- a/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java +++ b/src/tool/api/api_scan_IntelliJ_plugin/src/com/kh/scan/dialog/ApiScanDialogPane.java @@ -272,6 +272,7 @@ public class ApiScanDialogPane extends JDialog { */ private void writeTmpFile(String path, byte[] bs) throws IOException { File file = new File(path); + FileOutputStream fw = null; if (!file.exists()) { boolean isNewFile = file.createNewFile(); if (!isNewFile) { @@ -279,7 +280,7 @@ public class ApiScanDialogPane extends JDialog { } } try { - FileOutputStream fw = new FileOutputStream(file); + fw = new FileOutputStream(file); fw.write(bs, 0, bs.length); } catch (IOException e) { // 处理可能发生的IOException @@ -302,8 +303,10 @@ public class ApiScanDialogPane extends JDialog { BufferedReader stdInput = null; BufferedReader stdError = null; try { - stdInput = new BufferedReader(new InputStreamReader(process.getInputStream())); - stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); + stdInput = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); + stdError = new BufferedReader(new InputStreamReader(process.getErrorStream(), + StandardCharsets.UTF_8)); String sErr; String sOut; sErr = getErrorResult(stdError); @@ -393,26 +396,26 @@ public class ApiScanDialogPane extends JDialog { @Override public void run() { - InputStreamReader isr = null; - BufferedReader br = null; + InputStreamReader isr5 = null; + BufferedReader br5 = null; try { - isr = new InputStreamReader(is); - br = new BufferedReader(isr); + isr5 = new InputStreamReader(is, StandardCharsets.UTF_8); + br5 = new BufferedReader(isr5); String line; - while ((line = br.readLine()) != null) { + while ((line = br5.readLine()) != null) { LOG.info(line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); } finally { - // 确保BufferedReader br和InputStreamReader isr被关闭 + // 确保BufferedReader5 br和InputStreamReader isr5被关闭 try { - br.close(); + br5.close(); } catch (IOException e) { LOG.error(e); } try { - isr.close(); + isr5.close(); } catch (IOException e) { LOG.error(e); } @@ -434,19 +437,20 @@ public class ApiScanDialogPane extends JDialog { @Override public void run() { - BufferedReader br = null; + BufferedReader br2 = null; try { - br = new BufferedReader(new InputStreamReader(process.getInputStream())); + br2 = new BufferedReader(new InputStreamReader(process.getInputStream(), + StandardCharsets.UTF_8)); genResultLog(process); - while (br.readLine() != null) { + while (br2.readLine() != null) { LOG.info(" callExtProcess "); } } catch (IOException ioException) { LOG.error(" callExtProcess error" + ioException); } finally { - // 确保BufferedReader br被关闭 + // 确保BufferedReader br2被关闭 try { - br.close(); + br2.close(); } catch (IOException e) { // 处理关闭BufferedReader时的异常 LOG.error(e); -- Gitee From 356826f942dd1476be73ac426a5df30ac36f02fb Mon Sep 17 00:00:00 2001 From: huruitao Date: Tue, 16 Jul 2024 16:29:12 +0800 Subject: [PATCH 3/3] codecheck Signed-off-by: huruitao --- .../src/com/sk/gn/dialog/GenDialogPane.java | 26 +++++++++---------- .../src/com/sk/dialog/GenerateDialogPane.java | 14 +++++----- .../src/com/sk/na/ng/GenDTS.java | 16 ++++++------ .../dialog/ServiceGenerateDialogPane.java | 16 ++++++------ 4 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java index b3ce2983..c5260136 100644 --- a/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java +++ b/src/intellij_plugin/cmake2gn/gn_IntelliJ_plugin/src/com/sk/gn/dialog/GenDialogPane.java @@ -526,26 +526,26 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP @Override public void run() { - InputStreamReader isr = null; - BufferedReader br = null; + InputStreamReader isr1 = null; + BufferedReader br1 = null; try { - isr = new InputStreamReader(is, StandardCharsets.UTF_8); - br = new BufferedReader(isr); + isr1 = new InputStreamReader(is, StandardCharsets.UTF_8); + br1 = new BufferedReader(isr1); String line; - while ((line = br.readLine()) != null) { + while ((line = br1.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); } finally { - // 确保BufferedReader br和InputStreamReader isr被关闭 + // 确保BufferedReader br1和InputStreamReader isr1被关闭 try { - br.close(); + br1.close(); } catch (IOException e) { LOG.error(e); } try { - isr.close(); + isr1.close(); } catch (IOException e) { LOG.error(e); } @@ -567,20 +567,20 @@ public class GenDialogPane extends JDialog implements SelectOutDirAction.SelectP @Override public void run() { - BufferedReader br = null; + BufferedReader br1 = null; try { - br = new BufferedReader(new InputStreamReader(process.getInputStream(), + br1 = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8)); genResultLog(process); - while (br.readLine() != null) { + while (br1.readLine() != null) { LOG.info(" callExtProcess "); } } catch (IOException ioException) { LOG.error(" callExtProcess error" + ioException); } finally { - // 确保BufferedReader br被关闭 + // 确保BufferedReader br1被关闭 try { - br.close(); + br1.close(); } catch (IOException e) { // 处理关闭BufferedReader时的异常 LOG.error(e); diff --git a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java index b6e32bd1..80644469 100644 --- a/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java +++ b/src/intellij_plugin/dts2cpp/napi_IntelliJ_plugin/src/com/sk/dialog/GenerateDialogPane.java @@ -692,13 +692,13 @@ public class GenerateDialogPane extends JDialog { @Override public void run() { - InputStreamReader isr = null; - BufferedReader br = null; + InputStreamReader isr2 = null; + BufferedReader br2 = null; try { - isr = new InputStreamReader(is, StandardCharsets.UTF_8); - br = new BufferedReader(isr); + isr2 = new InputStreamReader(is, StandardCharsets.UTF_8); + br2 = new BufferedReader(isr2); String line; - while ((line = br.readLine()) != null) { + while ((line = br2.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { @@ -706,12 +706,12 @@ public class GenerateDialogPane extends JDialog { } finally { // 确保BufferedReader和InputStreamReader被关闭 try { - br.close(); + br2.close(); } catch (IOException e) { LOG.error(e); } try { - isr.close(); + isr2.close(); } catch (IOException e) { LOG.error(e); } diff --git a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java index cf1dd1cd..e06b136a 100644 --- a/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java +++ b/src/intellij_plugin/h2dtscpp/native_IntelliJ_plugin/src/com/sk/na/ng/GenDTS.java @@ -196,26 +196,26 @@ public class GenDts extends AnAction { @Override public void run() { - InputStreamReader isr = null; - BufferedReader br = null; + InputStreamReader isr3 = null; + BufferedReader br3 = null; try { - isr = new InputStreamReader(is, StandardCharsets.UTF_8); - br = new BufferedReader(isr); + isr3 = new InputStreamReader(is, StandardCharsets.UTF_8); + br3 = new BufferedReader(isr3); String line; - while ((line = br.readLine()) != null) { + while ((line = br3.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); } finally { - // 确保BufferedReader br和InputStreamReader isr被关闭 + // 确保BufferedReader br3和InputStreamReader isr3被关闭 try { - br.close(); + br3.close(); } catch (IOException e) { LOG.error(e); } try { - isr.close(); + isr3.close(); } catch (IOException e) { LOG.error(e); } diff --git a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java index a2f586c6..9a604723 100644 --- a/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java +++ b/src/intellij_plugin/h2sa/service_IntelliJ_plugin/src/com/sk/service/dialog/ServiceGenerateDialogPane.java @@ -402,26 +402,26 @@ public class ServiceGenerateDialogPane extends JDialog { @Override public void run() { - InputStreamReader isr = null; - BufferedReader br = null; + InputStreamReader isr4 = null; + BufferedReader br4 = null; try { - isr = new InputStreamReader(is, StandardCharsets.UTF_8); - br = new BufferedReader(isr); + isr4 = new InputStreamReader(is, StandardCharsets.UTF_8); + br4 = new BufferedReader(isr4); String line; - while ((line = br.readLine()) != null) { + while ((line = br4.readLine()) != null) { LOG.error("StreamConsumer" + line); } } catch (IOException ioException) { LOG.error("StreamConsumer io error" + ioException); } finally { - // 确保BufferedReader br和InputStreamReader isr被关闭 + // 确保BufferedReader br4和InputStreamReader isr4被关闭 try { - br.close(); + br4.close(); } catch (IOException e) { LOG.error(e); } try { - isr.close(); + isr4.close(); } catch (IOException e) { LOG.error(e); } -- Gitee