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 17:34:52 UTC
(maven-resolver) branch fix-1998-try-with-resources created (now cd608c1b3)
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a change to branch fix-1998-try-with-resources
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
at cd608c1b3 Fix #1998: use try-with-resources in test file utilities
This branch includes the following new commits:
new cd608c1b3 Fix #1998: use try-with-resources in test file utilities
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(maven-resolver) 01/01: Fix #1998: use try-with-resources in test file utilities
Posted by el...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
elharo pushed a commit to branch fix-1998-try-with-resources
in repository https://gitbox.apache.org/repos/asf/maven-resolver.git
commit cd608c1b3d386620ad451f2822dafe33d139ea9a
Author: Elliotte Rusty Harold <el...@ibiblio.org>
AuthorDate: Sun Aug 9 17:33:19 2026 +0000
Fix #1998: use try-with-resources in test file utilities
---
.../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.
- }
}
}
}