diff --git a/maven-resolver-test-http/src/main/java/org/eclipse/aether/internal/test/util/http/HttpServer.java b/maven-resolver-test-http/src/main/java/org/eclipse/aether/internal/test/util/http/HttpServer.java index dd938040d..4904eea40 100644 --- a/maven-resolver-test-http/src/main/java/org/eclipse/aether/internal/test/util/http/HttpServer.java +++ b/maven-resolver-test-http/src/main/java/org/eclipse/aether/internal/test/util/http/HttpServer.java @@ -424,9 +424,7 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS if (HttpMethod.HEAD.is(req.getMethod())) { return; } - FileInputStream is = null; - try { - is = new FileInputStream(file); + try (FileInputStream is = new FileInputStream(file)) { if (offset > 0L) { long skipped = is.skip(offset); while (skipped < offset && is.read() >= 0) { @@ -434,16 +432,6 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS } } IO.copy(is, response.getOutputStream()); - is.close(); - is = null; - } finally { - try { - if (is != null) { - is.close(); - } - } catch (final IOException e) { - // Suppressed due to an exception already thrown in the try block. - } } } else if (HttpMethod.PUT.is(req.getMethod())) { if (!webDav) { @@ -451,20 +439,8 @@ public void handle(String target, Request req, HttpServletRequest request, HttpS } if (file.getParentFile().exists()) { try { - FileOutputStream os = null; - try { - os = new FileOutputStream(file); + try (FileOutputStream os = new FileOutputStream(file)) { IO.copy(request.getInputStream(), os); - os.close(); - os = null; - } finally { - try { - if (os != null) { - os.close(); - } - } catch (final IOException e) { - // Suppressed due to an exception already thrown in the try block. - } } } catch (IOException e) { file.delete(); diff --git a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/DependencyGraphParser.java b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/DependencyGraphParser.java index 18da45bb7..be1a31317 100644 --- a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/DependencyGraphParser.java +++ b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/DependencyGraphParser.java @@ -172,19 +172,9 @@ public List parseMultiResource(String resource) throws IOExcepti * Parse the graph definition read from the given URL. */ public DependencyNode parse(URL resource) throws IOException { - BufferedReader reader = null; - try { - reader = new BufferedReader(new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8)); + try (BufferedReader reader = + new BufferedReader(new InputStreamReader(resource.openStream(), StandardCharsets.UTF_8))) { return parse(reader); - } finally { - try { - if (reader != null) { - reader.close(); - reader = null; - } - } catch (final IOException e) { - // Suppressed due to an exception already thrown in the try block. - } } } diff --git a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/IniArtifactDataReader.java b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/IniArtifactDataReader.java index c6979cde4..4f4ca7c27 100644 --- a/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/IniArtifactDataReader.java +++ b/maven-resolver-test-util/src/main/java/org/eclipse/aether/internal/test/util/IniArtifactDataReader.java @@ -96,9 +96,7 @@ private ArtifactDescription parse(Reader reader) throws IOException { Map> sections = new HashMap<>(); - BufferedReader in = null; - try { - in = new BufferedReader(reader); + try (BufferedReader in = new BufferedReader(reader)) { while ((line = in.readLine()) != null) { line = cutComment(line); @@ -123,17 +121,6 @@ private ArtifactDescription parse(Reader reader) throws IOException { lines.add(line.trim()); } } - - in.close(); - in = null; - } finally { - try { - if (in != null) { - in.close(); - } - } catch (final IOException e) { - // Suppressed due to an exception already thrown in the try block. - } } Artifact relocation = relocation(sections.get(State.RELOCATION)); 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..55fa5e644 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 boolean mkdirs(File directory) { 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 void copy(File source, File target) throws IOException { 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); + try (InputStream fis = new FileInputStream(source); + OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) { mkdirs(target.getAbsoluteFile().getParentFile()); - - 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..1ecdbc1b7 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,11 @@ public static File createTempDir(String suffix) throws IOException { public static long copyFile(File source, File target) throws IOException { long total = 0; - FileInputStream fis = null; - OutputStream fos = null; - try { - fis = new FileInputStream(source); + try (FileInputStream fis = new FileInputStream(source); + OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) { mkdirs(target.getParentFile()); - fos = new BufferedOutputStream(new FileOutputStream(target)); - for (byte[] buffer = new byte[1024 * 32]; ; ) { int bytes = fis.read(buffer); if (bytes < 0) { @@ -191,28 +187,6 @@ public static long copyFile(File source, File target) throws IOException { 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 +202,10 @@ public static long copyFile(File source, File target) throws IOException { */ @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 +213,10 @@ public static byte[] readBytes(File file) throws IOException { 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 +237,16 @@ public static void writeString(File file, String content, long timestamp) throws } 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. - } } } }