Clean up Java package
This commit is contained in:
parent
2393176fa1
commit
719a464687
|
@ -10,6 +10,7 @@
|
|||
|
||||
<name>hyperbuild</name>
|
||||
<description>Fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling</description>
|
||||
<url>https://github.com/wilsonzlin/hyperbuild</url>
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Wilson Lin</name>
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package in.wilsonl.hyperbuild;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
import static in.wilsonl.hyperbuild.NativeLibraryLoader.loadLibraryFromJar;
|
||||
import static java.lang.String.format;
|
||||
|
||||
public class Hyperbuild {
|
||||
|
@ -11,26 +14,40 @@ public class Hyperbuild {
|
|||
String osArch = System.getProperty("os.arch").toLowerCase();
|
||||
|
||||
String nativeLibNameOs = osName.startsWith("windows")
|
||||
? "windows"
|
||||
: osName.startsWith("linux")
|
||||
? "linux"
|
||||
: osName.startsWith("mac")
|
||||
? "macos"
|
||||
: null;
|
||||
? "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));
|
||||
throw new RuntimeException(format("Platform not supported (os.name=%s, os.arch=%s)", osName, osArch));
|
||||
}
|
||||
|
||||
try {
|
||||
loadLibraryFromJar(format("/%s-%s.nativelib", nativeLibNameOs, nativeLibNameArch));
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
public static native int minifyInPlace(ByteBuffer code) throws HyperbuildException;
|
||||
private Hyperbuild() {
|
||||
}
|
||||
|
||||
public static native String minify(String code) throws HyperbuildException;
|
||||
public static native int minifyInPlace(ByteBuffer code) throws SyntaxException;
|
||||
|
||||
public static native String minify(String code) throws SyntaxException;
|
||||
|
||||
public static class SyntaxException extends RuntimeException {
|
||||
private SyntaxException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +0,0 @@
|
|||
package in.wilsonl.hyperbuild;
|
||||
|
||||
public class HyperbuildException extends RuntimeException {
|
||||
public HyperbuildException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
|
@ -1,65 +0,0 @@
|
|||
/*
|
||||
* Code in this file is shortened from https://github.com/adamheinrich/native-utils/blob/e6ea34b98e433bc67f2ad82a69f8f023d764e846/src/main/java/cz/adamh/utils/NativeUtils.java.
|
||||
*
|
||||
* Class NativeUtils is published under the The MIT License:
|
||||
*
|
||||
* Copyright (c) 2012 Adam Heinrich <adam@adamh.cz>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
package in.wilsonl.hyperbuild;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.FileSystemNotFoundException;
|
||||
import java.nio.file.FileSystems;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.ProviderNotFoundException;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
public class NativeLibraryLoader {
|
||||
private NativeLibraryLoader() {
|
||||
}
|
||||
|
||||
public static void loadLibraryFromJar(String filename) throws IOException {
|
||||
File temp = File.createTempFile("hyperbuild-java-nativelib", filename.substring(1));
|
||||
|
||||
try (InputStream is = NativeLibraryLoader.class.getResourceAsStream(filename)) {
|
||||
Files.copy(is, temp.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
System.load(temp.getAbsolutePath());
|
||||
} finally {
|
||||
if (isPosixCompliant()) {
|
||||
// Assume POSIX compliant file system, can be deleted after loading.
|
||||
temp.delete();
|
||||
} else {
|
||||
// Assume non-POSIX, and don't delete until last file descriptor closed.
|
||||
temp.deleteOnExit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isPosixCompliant() {
|
||||
try {
|
||||
return FileSystems.getDefault().supportedFileAttributeViews().contains("posix");
|
||||
} catch (FileSystemNotFoundException | ProviderNotFoundException | SecurityException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -45,7 +45,7 @@ pub extern "system" fn Java_in_wilsonl_hyperbuild_Hyperbuild_minify(
|
|||
Ok(out_len) => env.new_string(unsafe { from_utf8_unchecked(&code[0..out_len]) }).unwrap().into_inner(),
|
||||
Err((err, pos)) => {
|
||||
env.throw_new(
|
||||
"in/wilsonl/hyperbuild/HyperbuildException",
|
||||
"in/wilsonl/hyperbuild/Hyperbuild$SyntaxException",
|
||||
format!("{} [Character {}]", err.message(), pos),
|
||||
).unwrap();
|
||||
JObject::null().into_inner()
|
||||
|
|
Loading…
Reference in New Issue