From 2ebbc0893baa735d589bff7018728ad88b1f4adc Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 4 Aug 2016 21:52:59 +0200 Subject: [PATCH] always close file descriptors, even on exceptions --- src/bindings/java/org/simgrid/NativeLib.java | 24 ++++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/bindings/java/org/simgrid/NativeLib.java b/src/bindings/java/org/simgrid/NativeLib.java index 6cb2a9e564..b14be8931f 100644 --- a/src/bindings/java/org/simgrid/NativeLib.java +++ b/src/bindings/java/org/simgrid/NativeLib.java @@ -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. */ + } + } } } -- 2.30.2