Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
always close file descriptors, even on exceptions
authorMartin Quinson <martin.quinson@loria.fr>
Thu, 4 Aug 2016 19:52:59 +0000 (21:52 +0200)
committerMartin Quinson <martin.quinson@loria.fr>
Thu, 4 Aug 2016 20:03:46 +0000 (22:03 +0200)
src/bindings/java/org/simgrid/NativeLib.java

index 6cb2a9e..b14be89 100644 (file)
@@ -93,7 +93,8 @@ public final class NativeLib {
 
                String filename=name;
                InputStream in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
-
+               OutputStream out = null;
+               
                if (in == null) {
                        filename = "lib"+name+".so";
                        in = NativeLib.class.getClassLoader().getResourceAsStream(path+filename);
@@ -123,18 +124,31 @@ public final class NativeLib {
                        File fileOut = new File(tempDir.toFile().getAbsolutePath() + File.separator + filename);
 
                        /* copy the library in position */  
-                       OutputStream out = new FileOutputStream(fileOut);
+                       out = new FileOutputStream(fileOut);
                        byte[] buffer = new byte[4096]; 
                        int bytesRead; 
                        while ((bytesRead = in.read(buffer)) != -1)     // Read until EOF
                                out.write(buffer, 0, bytesRead); 
 
-                       /* close all file descriptors, and load that shit */
-                       in.close();
-                       out.close();
+                       /* load that shit */
                        System.load(fileOut.getAbsolutePath());
                } catch (SecurityException|UnsatisfiedLinkError|IOException e) {
                        throw new LinkageException("Cannot load the bindings to the "+name+" library in path "+getPath(), e);
+               } finally {
+                       /* Always close all descriptors, no matter success or error */
+                       try { 
+                               in.close();
+                       } catch (IOException e) {
+                               /* Too bad. I dont care. */
+                       }
+                       
+                       if (out != null) {
+                               try { 
+                               out.close();
+                               } catch (IOException e) {
+                                       /* Too bad too. I dont care either. */
+                               }
+                       }
                }
        }