Skip to content

Commit 937f484

Browse files
committed
Simplify NativeUtils.loadFileFromJar(...)
1 parent 78525d3 commit 937f484

File tree

1 file changed

+23
-39
lines changed

1 file changed

+23
-39
lines changed
Lines changed: 23 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package ca.weblite.nativeutils;
22

3-
import java.io.File;
43
import java.io.FileNotFoundException;
5-
import java.io.FileOutputStream;
64
import java.io.IOException;
75
import java.io.InputStream;
86
import java.io.OutputStream;
7+
import java.nio.file.Files;
8+
import java.nio.file.Path;
9+
import java.nio.file.StandardOpenOption;
910

1011
/**
1112
* Simple library class for working with JNI (Java Native Interface)
@@ -45,73 +46,56 @@ public static void loadLibraryFromJar(String path) throws IOException {
4546
* @throws java.io.IOException if any.
4647
*/
4748
public static void loadLibraryFromJar(String path, Class<?> source) throws IOException {
48-
49-
5049
// Finally, load the library
51-
System.load(loadFileFromJar(path, source).getAbsolutePath());
50+
System.load(loadFileFromJar(path, source).toAbsolutePath().toString());
5251
}
5352

5453
/**
5554
* <p>loadFileFromJar.</p>
5655
*
5756
* @param path a {@link java.lang.String} object.
5857
* @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
6059
* @throws java.io.IOException if any.
6160
*/
62-
public static File loadFileFromJar(String path, Class<?> source) throws IOException {
61+
public static Path loadFileFromJar(String path, Class<?> source) throws IOException {
6362
if (!path.startsWith("/")) {
6463
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
6564
}
6665

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);
7067

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);
7879
}
7980

8081
// Check if the filename is okay
81-
if (filename == null || prefix.length() < 3) {
82+
if (prefix.length() < 3) {
8283
throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
8384
}
8485

8586
// 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();
9689

9790
// Open and check input stream
9891
InputStream is = source.getResourceAsStream(path);
9992
if (is == null) {
10093
throw new FileNotFoundException("File " + path + " was not found inside JAR.");
10194
}
10295

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);
11398
}
11499
return temp;
115-
116100
}
117101
}

0 commit comments

Comments
 (0)