|
1 | 1 | package ca.weblite.nativeutils; |
2 | 2 |
|
3 | | -import java.io.File; |
4 | 3 | import java.io.FileNotFoundException; |
5 | | -import java.io.FileOutputStream; |
6 | 4 | import java.io.IOException; |
7 | 5 | import java.io.InputStream; |
8 | 6 | import java.io.OutputStream; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.Path; |
| 9 | +import java.nio.file.StandardOpenOption; |
9 | 10 |
|
10 | 11 | /** |
11 | 12 | * Simple library class for working with JNI (Java Native Interface) |
@@ -45,73 +46,56 @@ public static void loadLibraryFromJar(String path) throws IOException { |
45 | 46 | * @throws java.io.IOException if any. |
46 | 47 | */ |
47 | 48 | public static void loadLibraryFromJar(String path, Class<?> source) throws IOException { |
48 | | - |
49 | | - |
50 | 49 | // Finally, load the library |
51 | | - System.load(loadFileFromJar(path, source).getAbsolutePath()); |
| 50 | + System.load(loadFileFromJar(path, source).toAbsolutePath().toString()); |
52 | 51 | } |
53 | 52 |
|
54 | 53 | /** |
55 | 54 | * <p>loadFileFromJar.</p> |
56 | 55 | * |
57 | 56 | * @param path a {@link java.lang.String} object. |
58 | 57 | * @param source a {@link java.lang.Class} object. |
59 | | - * @return a {@link java.io.File} object. |
| 58 | + * @return file path of the temporary file extracted from this JAR |
60 | 59 | * @throws java.io.IOException if any. |
61 | 60 | */ |
62 | | - public static File loadFileFromJar(String path, Class<?> source) throws IOException { |
| 61 | + public static Path loadFileFromJar(String path, Class<?> source) throws IOException { |
63 | 62 | if (!path.startsWith("/")) { |
64 | 63 | throw new IllegalArgumentException("The path has to be absolute (start with '/')."); |
65 | 64 | } |
66 | 65 |
|
67 | | - // Obtain filename from path |
68 | | - String[] parts = path.split("/"); |
69 | | - String filename = (parts.length > 1) ? parts[parts.length - 1] : null; |
| 66 | + String filename = path.substring(path.lastIndexOf('/') + 1); |
70 | 67 |
|
71 | | - // Split filename to prexif and suffix (extension) |
72 | | - String prefix = ""; |
73 | | - String suffix = null; |
74 | | - if (filename != null) { |
75 | | - parts = filename.split("\\.", 2); |
76 | | - prefix = parts[0]; |
77 | | - suffix = (parts.length > 1) ? "."+parts[parts.length - 1] : null; // Thanks, davs! :-) |
| 68 | + // Split filename to prefix and suffix (extension) |
| 69 | + String prefix; |
| 70 | + String suffix; |
| 71 | + int lastDot = filename.lastIndexOf('.'); |
| 72 | + if (lastDot == -1) { |
| 73 | + // No file extension; use complete filename as prefix |
| 74 | + prefix = filename; |
| 75 | + suffix = null; |
| 76 | + } else { |
| 77 | + prefix = filename.substring(0, lastDot); |
| 78 | + suffix = filename.substring(lastDot); |
78 | 79 | } |
79 | 80 |
|
80 | 81 | // Check if the filename is okay |
81 | | - if (filename == null || prefix.length() < 3) { |
| 82 | + if (prefix.length() < 3) { |
82 | 83 | throw new IllegalArgumentException("The filename has to be at least 3 characters long."); |
83 | 84 | } |
84 | 85 |
|
85 | 86 | // Prepare temporary file |
86 | | - File temp = File.createTempFile(prefix, suffix); |
87 | | - temp.deleteOnExit(); |
88 | | - |
89 | | - if (!temp.exists()) { |
90 | | - throw new FileNotFoundException("File " + temp.getAbsolutePath() + " does not exist."); |
91 | | - } |
92 | | - |
93 | | - // Prepare buffer for data copying |
94 | | - byte[] buffer = new byte[1024]; |
95 | | - int readBytes; |
| 87 | + Path temp = Files.createTempFile(prefix, suffix); |
| 88 | + temp.toFile().deleteOnExit(); |
96 | 89 |
|
97 | 90 | // Open and check input stream |
98 | 91 | InputStream is = source.getResourceAsStream(path); |
99 | 92 | if (is == null) { |
100 | 93 | throw new FileNotFoundException("File " + path + " was not found inside JAR."); |
101 | 94 | } |
102 | 95 |
|
103 | | - // Open output stream and copy data between source file in JAR and the temporary file |
104 | | - OutputStream os = new FileOutputStream(temp); |
105 | | - try { |
106 | | - while ((readBytes = is.read(buffer)) != -1) { |
107 | | - os.write(buffer, 0, readBytes); |
108 | | - } |
109 | | - } finally { |
110 | | - // If read/write fails, close streams safely before throwing an exception |
111 | | - os.close(); |
112 | | - is.close(); |
| 96 | + try (is; OutputStream out = Files.newOutputStream(temp, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) { |
| 97 | + is.transferTo(out); |
113 | 98 | } |
114 | 99 | return temp; |
115 | | - |
116 | 100 | } |
117 | 101 | } |
0 commit comments