diff --git a/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java b/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java index 292f650..4f029a8 100644 --- a/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java +++ b/src/main/java/rocks/kavin/reqwest4j/ReqwestUtils.java @@ -1,39 +1,37 @@ package rocks.kavin.reqwest4j; import java.io.File; +import java.io.FileOutputStream; import java.io.IOException; import java.util.Map; public class ReqwestUtils { static { - String arch; + String arch = switch (System.getProperty("os.arch")) { + case "aarch64" -> "aarch64"; + case "amd64" -> "x86_64"; + default -> throw new RuntimeException("Unsupported architecture"); + }; - switch (System.getProperty("os.arch")) { - case "aarch64": - arch = "aarch64"; - break; - case "amd64": - arch = "x86_64"; - break; - default: - throw new RuntimeException("Unsupported architecture"); - } + File nativeFile; - String fileName = - System.getProperty("java.io.tmpdir") + - File.separatorChar + - "libreqwest_" + System.currentTimeMillis() + ".so"; - - final var cl = ReqwestUtils.class.getClassLoader(); - - try (var stream = cl.getResourceAsStream("META-INF/natives/linux/" + arch + "/libreqwest.so")) { - stream.transferTo(new java.io.FileOutputStream(fileName)); + try { + nativeFile = File.createTempFile("libreqwest", ".so"); + nativeFile.deleteOnExit(); } catch (IOException e) { throw new RuntimeException(e); } - System.load(fileName); + final var cl = ReqwestUtils.class.getClassLoader(); + + try (var stream = cl.getResourceAsStream("META-INF/natives/linux/" + arch + "/libreqwest.so")) { + stream.transferTo(new FileOutputStream(nativeFile)); + } catch (IOException e) { + throw new RuntimeException(e); + } + + System.load(nativeFile.getAbsolutePath()); } public static native Response fetch(String url, String method, byte[] body,