From 9e7b4aa083d75bd348d47c1a316189062fd8805a Mon Sep 17 00:00:00 2001 From: Edwin Stang Date: Wed, 10 Apr 2024 10:31:19 +0200 Subject: [PATCH 1/2] https://github.com/lz4/lz4-java/issues/218 add option to prefer java version in LZ4Factory.fastestInstance --- src/java/net/jpountz/lz4/LZ4Factory.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/java/net/jpountz/lz4/LZ4Factory.java b/src/java/net/jpountz/lz4/LZ4Factory.java index 1350c40..1e79f90 100644 --- a/src/java/net/jpountz/lz4/LZ4Factory.java +++ b/src/java/net/jpountz/lz4/LZ4Factory.java @@ -56,10 +56,16 @@ private static LZ4Factory instance(String impl) { } } + private static final boolean PREFER_NATIVE; private static LZ4Factory NATIVE_INSTANCE, JAVA_UNSAFE_INSTANCE, JAVA_SAFE_INSTANCE; + static { + final String preferNativeStr = System.getProperty("net.jpountz.lz4.PREFER_NATIVE"); + PREFER_NATIVE = preferNativeStr == null || Boolean.valueOf(preferNativeStr); + } + /** * Returns a {@link LZ4Factory} instance that returns compressors and * decompressors that are native bindings to the original C library. @@ -160,7 +166,7 @@ public static LZ4Factory fastestJavaInstance() { * @return the fastest available {@link LZ4Factory} instance */ public static LZ4Factory fastestInstance() { - if (Native.isLoaded() + if (PREFER_NATIVE && Native.isLoaded() || Native.class.getClassLoader() == ClassLoader.getSystemClassLoader()) { try { return nativeInstance(); From dea83eac38756224ed6d9811265c5e2590712766 Mon Sep 17 00:00:00 2001 From: Edwin Stang Date: Wed, 10 Apr 2024 11:00:29 +0200 Subject: [PATCH 2/2] fix condition for prefer native --- src/java/net/jpountz/lz4/LZ4Factory.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/java/net/jpountz/lz4/LZ4Factory.java b/src/java/net/jpountz/lz4/LZ4Factory.java index 1e79f90..31932fc 100644 --- a/src/java/net/jpountz/lz4/LZ4Factory.java +++ b/src/java/net/jpountz/lz4/LZ4Factory.java @@ -166,8 +166,8 @@ public static LZ4Factory fastestJavaInstance() { * @return the fastest available {@link LZ4Factory} instance */ public static LZ4Factory fastestInstance() { - if (PREFER_NATIVE && Native.isLoaded() - || Native.class.getClassLoader() == ClassLoader.getSystemClassLoader()) { + if (PREFER_NATIVE && (Native.isLoaded() + || Native.class.getClassLoader() == ClassLoader.getSystemClassLoader())) { try { return nativeInstance(); } catch (Throwable t) {