mirror of
				https://github.com/TeamPiped/reqwest4j.git
				synced 2024-08-14 23:54:39 +00:00 
			
		
		
		
	Cleanup tempDir file creation.
This commit is contained in:
		
							parent
							
								
									4480826b54
								
							
						
					
					
						commit
						1c58bc2dba
					
				
					 1 changed files with 19 additions and 21 deletions
				
			
		| 
						 | 
					@ -1,39 +1,37 @@
 | 
				
			||||||
package rocks.kavin.reqwest4j;
 | 
					package rocks.kavin.reqwest4j;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.File;
 | 
					import java.io.File;
 | 
				
			||||||
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.util.Map;
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class ReqwestUtils {
 | 
					public class ReqwestUtils {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    static {
 | 
					    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")) {
 | 
					        File nativeFile;
 | 
				
			||||||
            case "aarch64":
 | 
					 | 
				
			||||||
                arch = "aarch64";
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            case "amd64":
 | 
					 | 
				
			||||||
                arch = "x86_64";
 | 
					 | 
				
			||||||
                break;
 | 
					 | 
				
			||||||
            default:
 | 
					 | 
				
			||||||
                throw new RuntimeException("Unsupported architecture");
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        String fileName =
 | 
					        try {
 | 
				
			||||||
                System.getProperty("java.io.tmpdir") +
 | 
					            nativeFile = File.createTempFile("libreqwest", ".so");
 | 
				
			||||||
                        File.separatorChar +
 | 
					            nativeFile.deleteOnExit();
 | 
				
			||||||
                        "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));
 | 
					 | 
				
			||||||
        } catch (IOException e) {
 | 
					        } catch (IOException e) {
 | 
				
			||||||
            throw new RuntimeException(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,
 | 
					    public static native Response fetch(String url, String method, byte[] body,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue