Skip to content

Commit

Permalink
Improve native libraries loading on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
zb3 committed Dec 6, 2023
1 parent 4b85d0c commit f7afad5
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions src/pl/zb3/freej2me/NativeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
* b) We can't modify java.library.path, not in an official, forward-compatible manner..
*/
public class NativeLoader {
private static String TAG = "NativeLoader";
private static String libraryDir = null;
private static Set<String> loadedLibraries = new HashSet<>();

Expand All @@ -48,10 +49,9 @@ public static void loadLibrary(String libraryName) {
}

String sourceLibraryDir = String.format("natives/%s-%s", osName, arch);
System.out.println("sourcelibrarydir "+sourceLibraryDir);
File tempDir = createTempDirectory(osName.equals("windows"));

File tempDir = createTempDirectory();
System.out.println("created temp dir, path is "+tempDir);
System.out.println(TAG+": extracting native libraries to "+tempDir);

extract(sourceLibraryDir, tempDir);

Expand Down Expand Up @@ -86,18 +86,26 @@ public static void loadLibrary(String libraryName) {
loadedLibraries.add(libraryName);
}

private static File createTempDirectory() {
File tempDir = new File(System.getProperty("java.io.tmpdir"), "freej2me-" + System.nanoTime());
if (!tempDir.mkdir()) {
private static File createTempDirectory(boolean isWindows) {
// on Windows, we can't delete the directory on exit because the dll files
// are still loaded.. so we use a persistent location to avoid flooding the temp directory

String dirName = isWindows ? "freej2me-natives-tmp" : "freej2me-" + System.nanoTime();

File tempDir = new File(System.getProperty("java.io.tmpdir"), dirName);
if (!tempDir.exists() && !tempDir.mkdir()) {
throw new RuntimeException("Failed to create a temporary directory");
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
NativeLoader.deleteDirectory(tempDir);
} catch (Exception e) {
e.printStackTrace();
}
}));

if (!isWindows) {
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
try {
NativeLoader.deleteDirectory(tempDir);
} catch (Exception e) {
e.printStackTrace();
}
}));
}
return tempDir;
}

Expand Down Expand Up @@ -129,13 +137,23 @@ private static void extract(String directoryToExtract, File tempDir) {
Files.createDirectories(entryDest);
} else {
Files.createDirectories(entryDest.getParent());

try (InputStream is = jar.getInputStream(entry);
OutputStream os = Files.newOutputStream(entryDest)) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
} catch (FileSystemException e) {
// on Windows this might fail when there's another
// instance of freej2me running.
// so if the file already exists, let it be.
if (!Files.exists(entryDest)) {
throw e;
} else {
System.out.println(TAG+": assuming another instance is running");
}
}
}
}
Expand Down

0 comments on commit f7afad5

Please sign in to comment.