From 719a464687787a9989f638643d6841f517609dd8 Mon Sep 17 00:00:00 2001 From: Wilson Lin Date: Mon, 20 Jan 2020 10:09:52 +1100 Subject: [PATCH] Clean up Java package --- java/pom.xml | 1 + .../in/wilsonl/hyperbuild/Hyperbuild.java | 41 ++++++++---- .../hyperbuild/HyperbuildException.java | 7 -- .../hyperbuild/NativeLibraryLoader.java | 65 ------------------- java/src/main/rust/lib.rs | 2 +- 5 files changed, 31 insertions(+), 85 deletions(-) delete mode 100644 java/src/main/java/in/wilsonl/hyperbuild/HyperbuildException.java delete mode 100644 java/src/main/java/in/wilsonl/hyperbuild/NativeLibraryLoader.java diff --git a/java/pom.xml b/java/pom.xml index 24f8a45..0d99c03 100644 --- a/java/pom.xml +++ b/java/pom.xml @@ -10,6 +10,7 @@ hyperbuild Fast one-pass in-place HTML minifier written in Rust with context-aware whitespace handling + https://github.com/wilsonzlin/hyperbuild Wilson Lin diff --git a/java/src/main/java/in/wilsonl/hyperbuild/Hyperbuild.java b/java/src/main/java/in/wilsonl/hyperbuild/Hyperbuild.java index c26dc1f..c7d5a0e 100644 --- a/java/src/main/java/in/wilsonl/hyperbuild/Hyperbuild.java +++ b/java/src/main/java/in/wilsonl/hyperbuild/Hyperbuild.java @@ -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); + } + } } diff --git a/java/src/main/java/in/wilsonl/hyperbuild/HyperbuildException.java b/java/src/main/java/in/wilsonl/hyperbuild/HyperbuildException.java deleted file mode 100644 index 54f39c9..0000000 --- a/java/src/main/java/in/wilsonl/hyperbuild/HyperbuildException.java +++ /dev/null @@ -1,7 +0,0 @@ -package in.wilsonl.hyperbuild; - -public class HyperbuildException extends RuntimeException { - public HyperbuildException(String message) { - super(message); - } -} diff --git a/java/src/main/java/in/wilsonl/hyperbuild/NativeLibraryLoader.java b/java/src/main/java/in/wilsonl/hyperbuild/NativeLibraryLoader.java deleted file mode 100644 index ffa6ba2..0000000 --- a/java/src/main/java/in/wilsonl/hyperbuild/NativeLibraryLoader.java +++ /dev/null @@ -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 - * - * 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; - } - } -} diff --git a/java/src/main/rust/lib.rs b/java/src/main/rust/lib.rs index f063f00..be1e23f 100644 --- a/java/src/main/rust/lib.rs +++ b/java/src/main/rust/lib.rs @@ -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()