Clean up copying warning 83/104583/1
authorRobert Varga <robert.varga@pantheon.tech>
Fri, 24 Feb 2023 19:46:24 +0000 (20:46 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Fri, 24 Feb 2023 19:46:46 +0000 (20:46 +0100)
Use try-with-resources to eliminate a warning.

Change-Id: I2cdde13ac39185574d51e925fca52629abdf15e7
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
plugin/yang-maven-plugin/src/test/java/org/opendaylight/yangtools/yang2sources/plugin/ScannedDependencyTest.java

index ea0ef3863011e5566c7e134bd376a3e85a996da4..fc33f06cc87794129099baae079d375380f86e95 100644 (file)
@@ -67,30 +67,28 @@ public class ScannedDependencyTest {
     }
 
     private static void addSourceFileToTargetJar(final File source, final JarOutputStream target) throws IOException {
-        BufferedInputStream in = null;
-        try {
-            if (source.isDirectory()) {
-                String name = source.getPath().replace("\\", "/");
-                if (!name.isEmpty()) {
-                    if (!name.endsWith("/")) {
-                        name += "/";
-                    }
-                    final JarEntry entry = new JarEntry(name);
-                    entry.setTime(source.lastModified());
-                    target.putNextEntry(entry);
-                    target.closeEntry();
+        if (source.isDirectory()) {
+            String name = source.getPath().replace("\\", "/");
+            if (!name.isEmpty()) {
+                if (!name.endsWith("/")) {
+                    name += "/";
                 }
-                for (final File nestedFile : source.listFiles()) {
-                    addSourceFileToTargetJar(nestedFile, target);
-                }
-                return;
+                final JarEntry entry = new JarEntry(name);
+                entry.setTime(source.lastModified());
+                target.putNextEntry(entry);
+                target.closeEntry();
+            }
+            for (final File nestedFile : source.listFiles()) {
+                addSourceFileToTargetJar(nestedFile, target);
             }
+            return;
+        }
 
-            final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
-            entry.setTime(source.lastModified());
-            target.putNextEntry(entry);
-            in = new BufferedInputStream(new FileInputStream(source));
+        final JarEntry entry = new JarEntry(source.getPath().replace("\\", "/"));
+        entry.setTime(source.lastModified());
+        target.putNextEntry(entry);
 
+        try (var in = new BufferedInputStream(new FileInputStream(source))) {
             final byte[] buffer = new byte[1024];
             while (true) {
                 final int count = in.read(buffer);
@@ -100,10 +98,6 @@ public class ScannedDependencyTest {
                 target.write(buffer, 0, count);
             }
             target.closeEntry();
-        } finally {
-            if (in != null) {
-                in.close();
-            }
         }
     }
 }