From 5ad487e9b9198b7b77b590ff7d4060f5f1f1a65a Mon Sep 17 00:00:00 2001 From: Rafael Winterhalter Date: Mon, 8 Jul 2024 18:33:36 +0200 Subject: [PATCH] Avoid non-closed streams. --- .../net/bytebuddy/agent/VirtualMachine.java | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java index 61b7ed6882..255b74508b 100644 --- a/byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java +++ b/byte-buddy-agent/src/main/java/net/bytebuddy/agent/VirtualMachine.java @@ -2405,7 +2405,13 @@ public int getOwnerIdOf(File file) { file.getAbsolutePath()}); int attempts = this.attempts; boolean exited = false; - String line = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")).readLine(); + String line; + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); + try { + line = reader.readLine(); + } finally { + reader.close(); + } do { try { if (process.exitValue() != 0) { @@ -2479,11 +2485,15 @@ public int getOwnerIdOf(File file) { Process process = Runtime.getRuntime().exec(new String[]{"istat", file.getAbsolutePath()}); int attempts = this.attempts; boolean exited = false; - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); StringBuilder output = new StringBuilder(); - String line; - while ((line = bufferedReader.readLine()) != null) { - output.append(line).append("\n"); + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); + try { + String line; + while ((line = reader.readLine()) != null) { + output.append(line).append("\n"); + } + } finally { + reader.close(); } do { try { @@ -2563,7 +2573,13 @@ public int getOwnerIdOf(File file) { file.getAbsolutePath()}); int attempts = this.attempts; boolean exited = false; - String line = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")).readLine(); + String line; + BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(), "UTF-8")); + try { + line = reader.readLine(); + } finally { + reader.close(); + } do { try { if (process.exitValue() != 0) {