Cleanup tempDir file creation.

This commit is contained in:
Kavin 2023-06-06 00:16:17 +01:00
parent 4480826b54
commit 1c58bc2dba
No known key found for this signature in database
GPG key ID: 49451E4482CC5BCD

View file

@ -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,