From c2dab157d105b2d669aae0556a38a0fc380bcff3 Mon Sep 17 00:00:00 2001 From: Chibici Tiberiu Date: Thu, 26 Oct 2017 17:54:56 +0300 Subject: [PATCH 1/3] Put DLL in temporary directory instead of changing the name. --- src/main/java/cz/adamh/utils/NativeUtils.java | 46 ++++++++++--------- 1 file changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/cz/adamh/utils/NativeUtils.java b/src/main/java/cz/adamh/utils/NativeUtils.java index bc6f768..ede714f 100644 --- a/src/main/java/cz/adamh/utils/NativeUtils.java +++ b/src/main/java/cz/adamh/utils/NativeUtils.java @@ -66,27 +66,15 @@ public class NativeUtils { String[] parts = path.split("/"); String filename = (parts.length > 1) ? parts[parts.length - 1] : null; - // Split filename to prefix and suffix (extension) - String prefix = ""; - String suffix = null; - if (filename != null) { - parts = filename.split("\\.", 2); - prefix = parts[0]; - suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-) - } - // Check if the filename is okay - if (filename == null || prefix.length() < 3) { + if (filename == null || filename.length() < 3) { throw new IllegalArgumentException("The filename has to be at least 3 characters long."); } // Prepare temporary file - File temp = File.createTempFile(prefix, suffix); - - if (!temp.exists()) { - throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist."); - } - + File tempDir = createTempDirectory("nativeutils"); + File temp = new File(tempDir, filename); + boolean tempFileIsPosix = false; try { if (FileSystems.getDefault() @@ -108,7 +96,7 @@ public class NativeUtils { // Open and check input stream InputStream is = NativeUtils.class.getResourceAsStream(path); if (is == null) { - temp.delete(); + tempDir.delete(); throw new FileNotFoundException("File " + path + " was not found inside JAR."); } @@ -119,7 +107,8 @@ public class NativeUtils { os.write(buffer, 0, readBytes); } } catch (Throwable e) { - temp.delete(); + temp.delete(); + tempDir.delete(); throw e; } finally { // If read/write fails, close streams safely before throwing an exception @@ -131,10 +120,25 @@ public class NativeUtils { // Load the library System.load(temp.getAbsolutePath()); } finally { - if (tempFileIsPosix) + if (tempFileIsPosix) { temp.delete(); - else - temp.deleteOnExit(); + tempDir.delete(); + } + else { + temp.deleteOnExit(); + tempDir.deleteOnExit(); + } } } + + private static File createTempDirectory(String prefix) throws IOException + { + String tempDir = System.getProperty("java.io.tmpdir"); + File generatedDir = new File(tempDir, prefix + System.nanoTime()); + + if (!generatedDir.mkdir()) + throw new IOException("Failed to create temp directory " + generatedDir.getName()); + + return generatedDir; + } } From 5765834982ad812feabea211f09d3d3a8b255597 Mon Sep 17 00:00:00 2001 From: Chibici Tiberiu Date: Thu, 26 Oct 2017 18:57:35 +0300 Subject: [PATCH 2/3] Create a single temporary directory instead of one for each file. --- src/main/java/cz/adamh/utils/NativeUtils.java | 23 +++++++++---------- 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/main/java/cz/adamh/utils/NativeUtils.java b/src/main/java/cz/adamh/utils/NativeUtils.java index ede714f..43b9225 100644 --- a/src/main/java/cz/adamh/utils/NativeUtils.java +++ b/src/main/java/cz/adamh/utils/NativeUtils.java @@ -39,12 +39,15 @@ import java.nio.file.ProviderNotFoundException; */ public class NativeUtils { + private static File temporaryDir; + /** * Private constructor - this class will never be instanced */ private NativeUtils() { } + /** * Loads library from current JAR archive * @@ -72,8 +75,11 @@ public class NativeUtils { } // Prepare temporary file - File tempDir = createTempDirectory("nativeutils"); - File temp = new File(tempDir, filename); + if (temporaryDir == null) { + temporaryDir = createTempDirectory("nativeutils"); + temporaryDir.deleteOnExit(); + } + File temp = new File(temporaryDir, filename); boolean tempFileIsPosix = false; try { @@ -95,10 +101,8 @@ public class NativeUtils { // Open and check input stream InputStream is = NativeUtils.class.getResourceAsStream(path); - if (is == null) { - tempDir.delete(); + if (is == null) throw new FileNotFoundException("File " + path + " was not found inside JAR."); - } // Open output stream and copy data between source file in JAR and the temporary file OutputStream os = new FileOutputStream(temp); @@ -108,7 +112,6 @@ public class NativeUtils { } } catch (Throwable e) { temp.delete(); - tempDir.delete(); throw e; } finally { // If read/write fails, close streams safely before throwing an exception @@ -120,14 +123,10 @@ public class NativeUtils { // Load the library System.load(temp.getAbsolutePath()); } finally { - if (tempFileIsPosix) { + if (tempFileIsPosix) temp.delete(); - tempDir.delete(); - } - else { + else temp.deleteOnExit(); - tempDir.deleteOnExit(); - } } } From 157354034634441ed874233b4f012de767d43e5d Mon Sep 17 00:00:00 2001 From: Chibici Tiberiu Date: Thu, 26 Oct 2017 19:00:18 +0300 Subject: [PATCH 3/3] Fixed tabs. --- src/main/java/cz/adamh/utils/NativeUtils.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/main/java/cz/adamh/utils/NativeUtils.java b/src/main/java/cz/adamh/utils/NativeUtils.java index 43b9225..a311a6b 100644 --- a/src/main/java/cz/adamh/utils/NativeUtils.java +++ b/src/main/java/cz/adamh/utils/NativeUtils.java @@ -39,8 +39,8 @@ import java.nio.file.ProviderNotFoundException; */ public class NativeUtils { - private static File temporaryDir; - + private static File temporaryDir; + /** * Private constructor - this class will never be instanced */ @@ -76,8 +76,8 @@ public class NativeUtils { // Prepare temporary file if (temporaryDir == null) { - temporaryDir = createTempDirectory("nativeutils"); - temporaryDir.deleteOnExit(); + temporaryDir = createTempDirectory("nativeutils"); + temporaryDir.deleteOnExit(); } File temp = new File(temporaryDir, filename); @@ -111,7 +111,7 @@ public class NativeUtils { os.write(buffer, 0, readBytes); } } catch (Throwable e) { - temp.delete(); + temp.delete(); throw e; } finally { // If read/write fails, close streams safely before throwing an exception @@ -126,18 +126,18 @@ public class NativeUtils { if (tempFileIsPosix) temp.delete(); else - temp.deleteOnExit(); + temp.deleteOnExit(); } } private static File createTempDirectory(String prefix) throws IOException { - String tempDir = System.getProperty("java.io.tmpdir"); - File generatedDir = new File(tempDir, prefix + System.nanoTime()); - - if (!generatedDir.mkdir()) - throw new IOException("Failed to create temp directory " + generatedDir.getName()); - - return generatedDir; + String tempDir = System.getProperty("java.io.tmpdir"); + File generatedDir = new File(tempDir, prefix + System.nanoTime()); + + if (!generatedDir.mkdir()) + throw new IOException("Failed to create temp directory " + generatedDir.getName()); + + return generatedDir; } }