Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[sonar] Factor common expression and don't duplicate string literals.
[simgrid.git] / src / bindings / java / org / simgrid / NativeLib.java
index 757b8ef..6fb3c7f 100644 (file)
@@ -5,10 +5,8 @@
 
 package org.simgrid;
 
-import java.io.FileOutputStream;
 import java.io.InputStream;
 import java.io.IOException;
-import java.io.OutputStream;
 import java.io.File;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -20,6 +18,7 @@ import java.util.stream.Stream;
  * containing a call to this.
  */
 public final class NativeLib {
+       private static final boolean windowsOs = System.getProperty("os.name").toLowerCase().startsWith("win");
        private static boolean isNativeInited = false;
        private static Path tempDir = null; // where the embeeded libraries are unpacked before loading them
 
@@ -43,7 +42,7 @@ public final class NativeLib {
                if (isNativeInited)
                        return;
 
-               if (System.getProperty("os.name").toLowerCase().startsWith("win"))
+               if (windowsOs)
                        NativeLib.nativeInit("winpthread-1");
 
                NativeLib.nativeInit("simgrid");
@@ -91,20 +90,24 @@ public final class NativeLib {
                
                // We must write the lib onto the disk before loading it -- stupid operating systems
                if (tempDir == null) {
+                       final String tempPrefix = "simgrid-java-";
 
-                       if (System.getProperty("os.name").toLowerCase().startsWith("win")) {
+                       if (windowsOs) {
                                // The cleanup at exit fails on Windows where it is impossible to delete files which are still in
                                // use.  Try to remove stale temporary files from previous executions, and limit disk usage.
                                Path tmpdir = (new File(System.getProperty("java.io.tmpdir"))).toPath();
-                               Files.find(tmpdir, 1, (Path p, java.nio.file.attribute.BasicFileAttributes a) ->
-                                          a.isDirectory() && !p.equals(tmpdir) &&
-                                          p.getFileName().toString().startsWith("simgrid-java-"))
-                                    .map(Path::toFile)
-                                    .map(FileCleaner::new)
-                                    .forEach(FileCleaner::run);
+                               try (Stream<Path> paths = Files.find(tmpdir, 1,
+                                       (Path p, java.nio.file.attribute.BasicFileAttributes a) ->
+                                               a.isDirectory() && !p.equals(tmpdir) &&
+                                               p.getFileName().toString().startsWith(tempPrefix))) {
+                                       paths.map(Path::toFile)
+                                            .map(FileCleaner::new)
+                                            .forEach(FileCleaner::run);
+
+                               }
                        }
 
-                       tempDir = Files.createTempDirectory("simgrid-java-");
+                       tempDir = Files.createTempDirectory(tempPrefix);
                        // don't leak the files on disk, but remove it on JVM shutdown
                        Runtime.getRuntime().addShutdownHook(new Thread(new FileCleaner(tempDir.toFile())));
                }