Compare commits

...

5 Commits

Author SHA1 Message Date
Mohanned Anwar
b628ea28bc
Merge 2e150358ad into fb54d8b549 2023-05-22 17:37:53 -05:00
Mohanned Anwar
2e150358ad
Update NativeUtils.java 2023-04-02 08:56:57 +03:00
Mohanned Anwar
907ece2810
Update NativeUtils.java 2023-04-02 08:55:14 +03:00
Mohanned Anwar
20dacabd20
Update NativeUtils.java 2023-04-02 06:55:40 +03:00
Mohanned Anwar
76089a3466
Update NativeUtils.java 2023-04-02 05:34:32 +03:00

View File

@ -7,6 +7,7 @@ import java.io.InputStream;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.util.Locale; import java.util.Locale;
import java.util.Objects;
public final class NativeUtils { public final class NativeUtils {
@ -19,36 +20,33 @@ public final class NativeUtils {
} }
private static File unpackLibraryFromJarInternal(String path) throws IOException { private static File unpackLibraryFromJarInternal(String path) throws IOException {
if (null == path || !path.startsWith("/")) { Objects.requireNonNull(path, "The path cannot be null.");
throw new IllegalArgumentException("The path has to be absolute (start with '/')."); if (!path.startsWith("/")) {
throw new IllegalArgumentException("The path has to be absolute (start with '/').");
} }
String[] parts = path.split("/"); String[] parts = path.split("/");
String filename = (parts.length > 1) ? parts[parts.length - 1] : null; String filename = (parts.length > 1) ? parts[parts.length - 1] : null;
if (filename == null || filename.length() < MIN_PREFIX_LENGTH) { if (filename == null || filename.length() < MIN_PREFIX_LENGTH) {
throw new IllegalArgumentException("The filename has to be at least 3 characters long."); throw new IllegalArgumentException("The filename has to be at least 3 characters long.");
} }
if (temporaryDir == null) { File temp = Files.createTempFile(NATIVE_FOLDER_PATH_PREFIX, filename).toFile();
temporaryDir = createTempDirectory(NATIVE_FOLDER_PATH_PREFIX); temp.deleteOnExit();
temporaryDir.deleteOnExit();
}
File temp = new File(temporaryDir, filename);
try (InputStream is = NativeUtils.class.getResourceAsStream(path)) { try (InputStream is = NativeUtils.class.getResourceAsStream(path)) {
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) { } catch (IOException e) {
temp.delete(); temp.delete();
throw e; throw e;
} catch (NullPointerException e) { } catch (NullPointerException e) {
temp.delete(); temp.delete();
throw new FileNotFoundException("File " + path + " was not found inside JAR."); throw new FileNotFoundException("File " + path + " was not found inside JAR.");
} }
return temp; return temp;
} }
/** /**
* Unpack library from JAR into temporary path * Unpack library from JAR into temporary path