You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by el...@apache.org on 2026/08/09 20:36:54 UTC
(maven-resolver) branch master updated: Fix #1998: use try-with-resources in test file utilities (#2056)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
The following commit(s) were added to refs/heads/master by this push:
new df3b5d2ef Fix #1998: use try-with-resources in test file utilities (#2056)
df3b5d2ef is described below
commit df3b5d2ef1b7f746331f1c218d95b34e98034115
Author: Elliotte Rusty Harold <el...@users.noreply.github.com>
AuthorDate: Sun Aug 9 16:36:48 2026 -0400
Fix #1998: use try-with-resources in test file utilities (#2056)
---
.../internal/test/util/TestFileProcessor.java | 65 ++--------------
.../aether/internal/test/util/TestFileUtils.java | 89 ++--------------------
2 files changed, 12 insertions(+), 142 deletions(-)
diff --git a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileProcessor.java b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileProcessor.java
index 93ab4da4b..e3e00d567 100644
--- a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileProcessor.java
+++ b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileProcessor.java
@@ -63,46 +63,18 @@ public class TestFileProcessor implements FileProcessor {
public void write(File file, String data) throws IOException {
mkdirs(file.getParentFile());
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
-
+ try (FileOutputStream fos = new FileOutputStream(file)) {
if (data != null) {
fos.write(data.getBytes(StandardCharsets.UTF_8));
}
-
- fos.close();
- fos = null;
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
public void write(File target, InputStream source) throws IOException {
mkdirs(target.getAbsoluteFile().getParentFile());
- OutputStream fos = null;
- try {
- fos = new BufferedOutputStream(new FileOutputStream(target));
-
+ try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
copy(fos, source, null);
-
- fos.close();
- fos = null;
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
@@ -113,38 +85,11 @@ public class TestFileProcessor implements FileProcessor {
public long copy(File source, File target, ProgressListener listener) throws IOException {
long total = 0;
- InputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new FileInputStream(source);
-
- mkdirs(target.getAbsoluteFile().getParentFile());
-
- fos = new BufferedOutputStream(new FileOutputStream(target));
+ mkdirs(target.getAbsoluteFile().getParentFile());
+ try (InputStream fis = new FileInputStream(source);
+ OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
total = copy(fos, fis, listener);
-
- fos.close();
- fos = null;
-
- fis.close();
- fis = null;
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
- }
}
return total;
diff --git a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileUtils.java b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileUtils.java
index dcd7a6da3..1fd17d7d7 100644
--- a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileUtils.java
+++ b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/TestFileUtils.java
@@ -172,15 +172,10 @@ public class TestFileUtils {
public static long copyFile(File source, File target) throws IOException {
long total = 0;
- FileInputStream fis = null;
- OutputStream fos = null;
- try {
- fis = new FileInputStream(source);
-
- mkdirs(target.getParentFile());
-
- fos = new BufferedOutputStream(new FileOutputStream(target));
+ mkdirs(target.getParentFile());
+ try (FileInputStream fis = new FileInputStream(source);
+ OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
for (byte[] buffer = new byte[1024 * 32]; ; ) {
int bytes = fis.read(buffer);
if (bytes < 0) {
@@ -191,28 +186,6 @@ public class TestFileUtils {
total += bytes;
}
-
- fos.close();
- fos = null;
-
- fis.close();
- fis = null;
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
- }
}
return total;
@@ -228,22 +201,10 @@ public class TestFileUtils {
*/
@Deprecated
public static byte[] readBytes(File file) throws IOException {
- RandomAccessFile in = null;
- try {
- in = new RandomAccessFile(file, "r");
+ try (RandomAccessFile in = new RandomAccessFile(file, "r")) {
byte[] actual = new byte[(int) in.length()];
in.readFully(actual);
- in.close();
- in = null;
return actual;
- } finally {
- try {
- if (in != null) {
- in.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
@@ -251,22 +212,10 @@ public class TestFileUtils {
public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
file.deleteOnExit();
file.getParentFile().mkdirs();
- OutputStream out = null;
- try {
- out = new BufferedOutputStream(new FileOutputStream(file));
+ try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file))) {
for (int i = 0; i < repeat; i++) {
out.write(pattern);
}
- out.close();
- out = null;
- } finally {
- try {
- if (out != null) {
- out.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
@@ -287,40 +236,16 @@ public class TestFileUtils {
}
public static void readProps(File file, Properties props) throws IOException {
- FileInputStream fis = null;
- try {
- fis = new FileInputStream(file);
+ try (FileInputStream fis = new FileInputStream(file)) {
props.load(fis);
- fis.close();
- fis = null;
- } finally {
- try {
- if (fis != null) {
- fis.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
public static void writeProps(File file, Properties props) throws IOException {
file.getParentFile().mkdirs();
- FileOutputStream fos = null;
- try {
- fos = new FileOutputStream(file);
+ try (FileOutputStream fos = new FileOutputStream(file)) {
props.store(fos, "aether-test");
- fos.close();
- fos = null;
- } finally {
- try {
- if (fos != null) {
- fos.close();
- }
- } catch (final IOException e) {
- // Suppressed due to an exception already thrown in the try block.
- }
}
}
}