minify-html/java/src/main/java/in/wilsonl/hyperbuild/Hyperbuild.java

54 lines
1.7 KiB
Java
Raw Normal View History

2020-01-17 23:19:38 -05:00
package in.wilsonl.hyperbuild;
2020-01-19 18:09:52 -05:00
import java.io.File;
import java.io.InputStream;
2020-01-18 01:55:13 -05:00
import java.nio.ByteBuffer;
2020-01-19 18:09:52 -05:00
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
2020-01-17 23:19:38 -05:00
import static java.lang.String.format;
2020-01-17 23:19:38 -05:00
public class Hyperbuild {
static {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
String nativeLibNameOs = osName.startsWith("windows")
2020-01-19 18:09:52 -05:00
? "windows"
: osName.startsWith("linux")
? "linux"
: osName.startsWith("mac")
? "macos"
: null;
String nativeLibNameArch = osArch.equals("amd64") ? "x86_64" : null;
if (nativeLibNameOs == null || nativeLibNameArch == null) {
2020-01-19 18:09:52 -05:00
throw new RuntimeException(format("Platform not supported (os.name=%s, os.arch=%s)", osName, osArch));
}
2020-01-19 18:09:52 -05:00
String nativeLibFile = format("/%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch);
try (InputStream is = Hyperbuild.class.getResourceAsStream(nativeLibFile)) {
File temp = File.createTempFile("hyperbuild-java-nativelib", nativeLibFile.substring(1));
temp.deleteOnExit();
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
System.load(temp.getAbsolutePath());
} catch (Exception e) {
throw new RuntimeException("Failed to load native library", e);
}
2020-01-17 23:19:38 -05:00
}
2020-01-19 18:09:52 -05:00
private Hyperbuild() {
}
public static native int minifyInPlace(ByteBuffer code) throws SyntaxException;
2020-01-17 23:19:38 -05:00
2020-01-19 18:09:52 -05:00
public static native String minify(String code) throws SyntaxException;
public static class SyntaxException extends RuntimeException {
private SyntaxException(String message) {
super(message);
}
}
2020-01-17 23:19:38 -05:00
}