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

45 lines
1.4 KiB
Java
Raw Normal View History

2020-01-17 23:19:38 -05:00
package in.wilsonl.hyperbuild;
import java.util.Arrays;
import static in.wilsonl.hyperbuild.NativeLibraryLoader.loadLibraryFromJar;
import static java.lang.String.format;
2020-01-17 23:19:38 -05:00
import static java.nio.charset.StandardCharsets.UTF_8;
public class Hyperbuild {
static {
String osName = System.getProperty("os.name").toLowerCase();
String osArch = System.getProperty("os.arch").toLowerCase();
String nativeLibNameOs = osName.startsWith("windows")
? "windows"
: osName.startsWith("linux")
? "linux"
: osName.startsWith("mac")
? "macos"
: null;
String nativeLibNameArch = osArch.equals("amd64") ? "x86_64" : null;
if (nativeLibNameOs == null || nativeLibNameArch == null) {
throw new RuntimeException(format("Platform not supported (%s, %s)", osName, osArch));
}
try {
loadLibraryFromJar(format("%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch));
} catch (Exception e) {
throw new RuntimeException("Failed to load native library", e);
}
2020-01-17 23:19:38 -05:00
}
public static native int minifyInPlace(byte[] code) throws HyperbuildException;
public static byte[] minify(byte[] code) throws HyperbuildException {
int size = minifyInPlace(code);
return Arrays.copyOf(code, size);
}
public static String minify(String code) throws HyperbuildException {
return new String(Hyperbuild.minify(code.getBytes(UTF_8)), UTF_8);
}
}