Blame SOURCES/0-remove-unsafe.patch

8041ed
diff --git a/.classpath b/.classpath
8041ed
index 5ee3f97..058b340 100644
8041ed
--- a/.classpath
8041ed
+++ b/.classpath
8041ed
@@ -2,7 +2,6 @@
8041ed
 <classpath>
8041ed
 	<classpathentry kind="src" path="src/java"/>
8041ed
 	<classpathentry kind="src" path="build/java"/>
8041ed
-	<classpathentry kind="src" path="src/java-unsafe"/>
8041ed
 	<classpathentry kind="src" path="src/test-resources"/>
8041ed
 	<classpathentry kind="src" path="build/jni"/>
8041ed
 	<classpathentry kind="src" path="src/test"/>
8041ed
diff --git a/CHANGES.md b/CHANGES.md
8041ed
index c7eed89..b71dc35 100644
8041ed
--- a/CHANGES.md
8041ed
+++ b/CHANGES.md
8041ed
@@ -140,12 +140,6 @@
8041ed
  - [#99](https://github.com/lz4/lz4-java/pull/99)
8041ed
    LZ4FrameInputStream allows EndMark to be incompressible. (Charles Allen)
8041ed
 
8041ed
- - [#95](https://github.com/lz4/lz4-java/pull/95)
8041ed
-   Added unsafe instance support for aarch64. (Yuqi Gu)
8041ed
-
8041ed
- - [#93](https://github.com/lz4/lz4-java/pull/93)
8041ed
-   Added unsafe instance support for ppc64le. (Madhusudanan Kandasamy)
8041ed
-
8041ed
  - [#90](https://github.com/lz4/lz4-java/issues/90)
8041ed
    LZ4 Java now supports 64-bit JNI build on Solaris. (cndcourt)
8041ed
 
8041ed
@@ -207,11 +201,6 @@
8041ed
  - [#39](https://github.com/jpountz/lz4-java/pull/39)
8041ed
    The JAR is now a valid OSGI bundle. (Simon Chemouil)
8041ed
 
8041ed
- - [#33](https://github.com/jpountz/lz4-java/pull/33)
8041ed
-   The implementation based on Java's sun.misc.Unsafe relies on unaligned
8041ed
-   memory access and is now only used on platforms that support it.
8041ed
-   (Dmitry Shohov)
8041ed
-
8041ed
 
8041ed
 ## 1.2.0
8041ed
 
8041ed
diff --git a/README.md b/README.md
8041ed
index 9a34ea6..b966d9a 100644
8041ed
--- a/README.md
8041ed
+++ b/README.md
8041ed
@@ -24,12 +24,10 @@ decompressor instance.
8041ed
 
8041ed
 ## Implementations
8041ed
 
8041ed
-For LZ4 compressors, LZ4 HC compressors and decompressors, 3 implementations are
8041ed
+For LZ4 compressors, LZ4 HC compressors and decompressors, 2 implementations are
8041ed
 available:
8041ed
  - JNI bindings to the original C implementation by Yann Collet,
8041ed
  - a pure Java port of the compression and decompression algorithms,
8041ed
- - a Java port that uses the sun.misc.Unsafe API in order to achieve compression
8041ed
-   and decompression speeds close to the C implementation.
8041ed
 
8041ed
 Have a look at LZ4Factory for more information.
8041ed
 
8041ed
@@ -94,8 +92,7 @@ score of 10) hash function.
8041ed
 
8041ed
 ## Implementations
8041ed
 
8041ed
-Similarly to LZ4, 3 implementations are available: JNI bindings, pure Java port
8041ed
-and pure Java port that uses sun.misc.Unsafe.
8041ed
+Unsimilarly to LZ4, 2 implementations are available: JNI bindings, pure Java port
8041ed
 
8041ed
 Have a look at XXHashFactory for more information.
8041ed
 
8041ed
@@ -131,7 +128,7 @@ int hash = hash32.getValue();
8041ed
 
8041ed
 You can download released artifacts from [Maven Central](https://search.maven.org/search?q=g:org.lz4%20a:lz4-java).
8041ed
 
8041ed
-You can download pure-Java lz4-java from [Maven Central](https://search.maven.org/search?q=g:org.lz4%20a:lz4-pure-java). These artifacts include the Safe and Unsafe Java versions but not JNI bindings. (Experimental)
8041ed
+You can download pure-Java lz4-java from [Maven Central](https://search.maven.org/search?q=g:org.lz4%20a:lz4-pure-java). These artifacts include the Safe Java versions but not JNI bindings. (Experimental)
8041ed
 
8041ed
 # Documentation
8041ed
 
8041ed
@@ -172,10 +169,8 @@ Then run `ant`. It will:
8041ed
    located under `src/build`,
8041ed
  - compile the lz4 and xxhash libraries and their JNI (Java Native Interface)
8041ed
    bindings,
8041ed
- - compile Java sources in `src/java` (normal sources), `src/java-unsafe`
8041ed
-   (sources that make use of `sun.misc.Unsafe`) and `build/java`
8041ed
-   (auto-generated sources) to `build/classes`, `build/unsafe-classes` and
8041ed
-   `build/generated-classes`,
8041ed
+ - compile Java sources in `src/java` (normal sources) and `build/java`
8041ed
+   (auto-generated sources) to `build/classes` and `build/generated-classes`,
8041ed
  - generate a JAR file called lz4-${version}.jar under the `dist` directory.
8041ed
 
8041ed
 The JAR file that is generated contains Java class files, the native library
8041ed
diff --git a/build.xml b/build.xml
8041ed
index 2a08ad8..1d4cff5 100644
8041ed
--- a/build.xml
8041ed
+++ b/build.xml
8041ed
@@ -148,16 +148,6 @@
8041ed
       debug="true"
8041ed
       destdir="${build}/classes"
8041ed
       nativeHeaderDir="${build}/jni-headers"/>
8041ed
-    <mkdir dir="${build}/unsafe-classes" />
8041ed
-    
8041ed
-      includeAntRuntime="false"
8041ed
-      srcdir="${src}/java-unsafe"
8041ed
-      source="${javac.source}"
8041ed
-      target="${javac.target}"
8041ed
-      encoding="UTF-8"
8041ed
-      debug="true"
8041ed
-      classpath="${build}/classes"
8041ed
-      destdir="${build}/unsafe-classes"/>
8041ed
     <mkdir dir="${build}/generated-classes" />
8041ed
     
8041ed
       includeAntRuntime="false"
8041ed
@@ -169,7 +159,6 @@
8041ed
       destdir="${build}/generated-classes">
8041ed
       <classpath>
8041ed
         <pathelement location="${build}/classes/" />
8041ed
-        <pathelement location="${build}/unsafe-classes" />
8041ed
       </classpath>
8041ed
     </javac>
8041ed
   </target>
8041ed
@@ -234,7 +223,6 @@
8041ed
 
8041ed
   <path id="compile.tests.classpath">
8041ed
     <pathelement path="${build}/classes" />
8041ed
-    <pathelement path="${build}/unsafe-classes" />
8041ed
     <pathelement path="${build}/generated-classes" />
8041ed
     <fileset dir="lib" includes="*.jar" />
8041ed
   </path>
8041ed
@@ -249,7 +237,6 @@
8041ed
     <pathelement path="${src}/resources" />
8041ed
     <pathelement path="${src}/test-resources" />
8041ed
     <pathelement path="${build}/classes" />
8041ed
-    <pathelement path="${build}/unsafe-classes" />
8041ed
     <pathelement path="${build}/generated-classes" />
8041ed
     <pathelement path="${build}/test-classes" />
8041ed
     <fileset dir="${lib}" includes="*.jar" />
8041ed
@@ -317,7 +304,6 @@
8041ed
     
8041ed
       destfile="${dist}/${ivy.module}-${ivy.revision}-sources.jar">
8041ed
       <fileset dir="${src}/java" />
8041ed
-      <fileset dir="${src}/java-unsafe" />
8041ed
     </jar>
8041ed
   </target>
8041ed
 
8041ed
@@ -351,7 +337,6 @@
8041ed
       destfile="${dist}/${ivy.module}.jar">
8041ed
       <fileset dir="${src}/resources" erroronmissingdir="false" />
8041ed
       <fileset dir="${build}/classes" />
8041ed
-      <fileset dir="${build}/unsafe-classes" />
8041ed
       <fileset dir="${build}/generated-classes" />
8041ed
       <fileset dir="${build}/jni" erroronmissingdir="false" />
8041ed
       <manifest>
8041ed
@@ -365,7 +350,6 @@
8041ed
     
8041ed
       destfile="${dist}/${ivy.module}.jar">
8041ed
       <fileset dir="${build}/classes" />
8041ed
-      <fileset dir="${build}/unsafe-classes" />
8041ed
       <fileset dir="${build}/generated-classes" />
8041ed
       <manifest>
8041ed
 	<attribute name="Automatic-Module-Name" value="org.lz4.pure.java"/>
8041ed
@@ -411,15 +395,12 @@
8041ed
 
8041ed
   <target name="forbidden-apis" description="check API usage" depends="install-forbidden-apis,compile">
8041ed
     <forbidden-apis internalRuntimeForbidden="true" classpathref="forbidden-apis.classpath">
8041ed
-      <bundledSignatures name="jdk-unsafe-${javac.target}"/>
8041ed
       <bundledSignatures name="jdk-deprecated-${javac.target}"/>
8041ed
       <fileset dir="${build}/classes" includes="**/*.class" />
8041ed
     </forbidden-apis>
8041ed
     <forbidden-apis classpathref="forbidden-apis.classpath">
8041ed
-      <bundledSignatures name="jdk-unsafe-${javac.target}"/>
8041ed
       <bundledSignatures name="jdk-deprecated-${javac.target}"/>
8041ed
       <fileset dir="${build}/classes" includes="**/*.class" />
8041ed
-      <fileset dir="${build}/unsafe-classes" includes="**/*.class" />
8041ed
     </forbidden-apis>
8041ed
   </target>
8041ed
 
8041ed
diff --git a/src/build/gen_sources.mvel b/src/build/gen_sources.mvel
8041ed
index 0f32671..43ebb78 100644
8041ed
--- a/src/build/gen_sources.mvel
8041ed
+++ b/src/build/gen_sources.mvel
8041ed
@@ -24,7 +24,7 @@ def dest_file(path) {
8041ed
 
8041ed
 def generate_decompressors() {
8041ed
   compiledTemplate = get_template("decompressor.template");
8041ed
-  for (type : ["Safe", "Unsafe"]) {
8041ed
+  for (type : ["Safe"]) {
8041ed
     for (size : ["Fast", "Safe"]) {
8041ed
       dest = dest_file("lz4/LZ4Java" + type + size + "Decompressor.java");
8041ed
       args = new HashMap();
8041ed
@@ -37,7 +37,7 @@ def generate_decompressors() {
8041ed
 
8041ed
 def generate_compressors() {
8041ed
   compiledTemplate = get_template("compressor.template");
8041ed
-  for (type : ["Safe", "Unsafe"]) {
8041ed
+  for (type : ["Safe"]) {
8041ed
     dest = dest_file("lz4/LZ4Java" + type + "Compressor.java");
8041ed
     args = new HashMap();
8041ed
     args.put("type", type);
8041ed
@@ -47,7 +47,7 @@ def generate_compressors() {
8041ed
 
8041ed
 def generate_hc_compressors() {
8041ed
   compiledTemplate = get_template("compressor_hc.template");
8041ed
-  for (type : ["Safe", "Unsafe"]) {
8041ed
+  for (type : ["Safe"]) {
8041ed
     dest = dest_file("lz4/LZ4HCJava" + type + "Compressor.java");
8041ed
     args = new HashMap();
8041ed
     args.put("type", type);
8041ed
@@ -58,7 +58,7 @@ def generate_hc_compressors() {
8041ed
 def generate_xxhash() {
8041ed
   for (bitness : ["32", "64"]) {
8041ed
     compiledTemplate = get_template("xxhash" + bitness + ".template");
8041ed
-    for (type : ["Safe", "Unsafe"]) {
8041ed
+    for (type : ["Safe"]) {
8041ed
       dest = dest_file("xxhash/XXHash" + bitness + "Java" + type + ".java");
8041ed
       args = new HashMap();
8041ed
       args.put("type", type);
8041ed
@@ -70,7 +70,7 @@ def generate_xxhash() {
8041ed
 def generate_streaming_xxhash() {
8041ed
   for (bitness : ["32", "64"]) {
8041ed
     compiledTemplate = get_template("xxhash" + bitness + "_streaming.template");
8041ed
-    for (type : ["Safe", "Unsafe"]) {
8041ed
+    for (type : ["Safe"]) {
8041ed
       dest = dest_file("xxhash/StreamingXXHash" + bitness + "Java" + type + ".java");
8041ed
       args = new HashMap();
8041ed
       args.put("type", type);
8041ed
diff --git a/src/java-unsafe/net/jpountz/lz4/LZ4UnsafeUtils.java b/src/java-unsafe/net/jpountz/lz4/LZ4UnsafeUtils.java
8041ed
deleted file mode 100644
8041ed
index a5ad783..0000000
8041ed
--- a/src/java-unsafe/net/jpountz/lz4/LZ4UnsafeUtils.java
8041ed
+++ /dev/null
8041ed
@@ -1,206 +0,0 @@
8041ed
-package net.jpountz.lz4;
8041ed
-
8041ed
-/*
8041ed
- * Licensed under the Apache License, Version 2.0 (the "License");
8041ed
- * you may not use this file except in compliance with the License.
8041ed
- * You may obtain a copy of the License at
8041ed
- *
8041ed
- *     http://www.apache.org/licenses/LICENSE-2.0
8041ed
- *
8041ed
- * Unless required by applicable law or agreed to in writing, software
8041ed
- * distributed under the License is distributed on an "AS IS" BASIS,
8041ed
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8041ed
- * See the License for the specific language governing permissions and
8041ed
- * limitations under the License.
8041ed
- */
8041ed
-
8041ed
-import static net.jpountz.lz4.LZ4Constants.COPY_LENGTH;
8041ed
-import static net.jpountz.lz4.LZ4Constants.LAST_LITERALS;
8041ed
-import static net.jpountz.lz4.LZ4Constants.ML_BITS;
8041ed
-import static net.jpountz.lz4.LZ4Constants.ML_MASK;
8041ed
-import static net.jpountz.lz4.LZ4Constants.RUN_MASK;
8041ed
-import static net.jpountz.util.UnsafeUtils.readByte;
8041ed
-import static net.jpountz.util.UnsafeUtils.readInt;
8041ed
-import static net.jpountz.util.UnsafeUtils.readLong;
8041ed
-import static net.jpountz.util.UnsafeUtils.readShort;
8041ed
-import static net.jpountz.util.UnsafeUtils.writeByte;
8041ed
-import static net.jpountz.util.UnsafeUtils.writeInt;
8041ed
-import static net.jpountz.util.UnsafeUtils.writeLong;
8041ed
-import static net.jpountz.util.UnsafeUtils.writeShort;
8041ed
-import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;
8041ed
-
8041ed
-import java.nio.ByteOrder;
8041ed
-
8041ed
-enum LZ4UnsafeUtils {
8041ed
-  ;
8041ed
-
8041ed
-  static void safeArraycopy(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
8041ed
-    final int fastLen = len & 0xFFFFFFF8;
8041ed
-    wildArraycopy(src, srcOff, dest, destOff, fastLen);
8041ed
-    for (int i = 0, slowLen = len & 0x7; i < slowLen; i += 1) {
8041ed
-      writeByte(dest, destOff + fastLen + i, readByte(src, srcOff + fastLen + i));
8041ed
-    }
8041ed
-  }
8041ed
-
8041ed
-  static void wildArraycopy(byte[] src, int srcOff, byte[] dest, int destOff, int len) {
8041ed
-    for (int i = 0; i < len; i += 8) {
8041ed
-      writeLong(dest, destOff + i, readLong(src, srcOff + i));
8041ed
-    }
8041ed
-  }
8041ed
-
8041ed
-  static void wildIncrementalCopy(byte[] dest, int matchOff, int dOff, int matchCopyEnd) {
8041ed
-    if (dOff - matchOff < 4) {
8041ed
-      for (int i = 0; i < 4; ++i) {
8041ed
-        writeByte(dest, dOff+i, readByte(dest, matchOff+i));
8041ed
-      }
8041ed
-      dOff += 4;
8041ed
-      matchOff += 4;
8041ed
-      int dec = 0;
8041ed
-      assert dOff >= matchOff && dOff - matchOff < 8;
8041ed
-      switch (dOff - matchOff) {
8041ed
-      case 1:
8041ed
-        matchOff -= 3;
8041ed
-        break;
8041ed
-      case 2:
8041ed
-        matchOff -= 2;
8041ed
-        break;
8041ed
-      case 3:
8041ed
-        matchOff -= 3;
8041ed
-        dec = -1;
8041ed
-        break;
8041ed
-      case 5:
8041ed
-        dec = 1;
8041ed
-        break;
8041ed
-      case 6:
8041ed
-        dec = 2;
8041ed
-        break;
8041ed
-      case 7:
8041ed
-        dec = 3;
8041ed
-        break;
8041ed
-      default:
8041ed
-        break;
8041ed
-      }
8041ed
-      writeInt(dest, dOff, readInt(dest, matchOff));
8041ed
-      dOff += 4;
8041ed
-      matchOff -= dec;
8041ed
-    } else if (dOff - matchOff < COPY_LENGTH) {
8041ed
-      writeLong(dest, dOff, readLong(dest, matchOff));
8041ed
-      dOff += dOff - matchOff;
8041ed
-    }
8041ed
-    while (dOff < matchCopyEnd) {
8041ed
-      writeLong(dest, dOff, readLong(dest, matchOff));
8041ed
-      dOff += 8;
8041ed
-      matchOff += 8;
8041ed
-    }
8041ed
-  }
8041ed
-
8041ed
-  static void safeIncrementalCopy(byte[] dest, int matchOff, int dOff, int matchLen) {
8041ed
-    for (int i = 0; i < matchLen; ++i) {
8041ed
-      dest[dOff + i] = dest[matchOff + i];
8041ed
-      writeByte(dest, dOff + i, readByte(dest, matchOff + i));
8041ed
-    }
8041ed
-  }
8041ed
-
8041ed
-  static int readShortLittleEndian(byte[] src, int srcOff) {
8041ed
-    short s = readShort(src, srcOff);
8041ed
-    if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-      s = Short.reverseBytes(s);
8041ed
-    }
8041ed
-    return s & 0xFFFF;
8041ed
-  }
8041ed
-
8041ed
-  static void writeShortLittleEndian(byte[] dest, int destOff, int value) {
8041ed
-    short s = (short) value;
8041ed
-    if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-      s = Short.reverseBytes(s);
8041ed
-    }
8041ed
-    writeShort(dest, destOff, s);
8041ed
-  }
8041ed
-
8041ed
-  static boolean readIntEquals(byte[] src, int ref, int sOff) {
8041ed
-    return readInt(src, ref) == readInt(src, sOff);
8041ed
-  }
8041ed
-
8041ed
-  static int commonBytes(byte[] src, int ref, int sOff, int srcLimit) {
8041ed
-    int matchLen = 0;
8041ed
-    while (sOff <= srcLimit - 8) {
8041ed
-      if (readLong(src, sOff) == readLong(src, ref)) {
8041ed
-        matchLen += 8;
8041ed
-        ref += 8;
8041ed
-        sOff += 8;
8041ed
-      } else {
8041ed
-        final int zeroBits;
8041ed
-        if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-          zeroBits = Long.numberOfLeadingZeros(readLong(src, sOff) ^ readLong(src, ref));
8041ed
-        } else {
8041ed
-          zeroBits = Long.numberOfTrailingZeros(readLong(src, sOff) ^ readLong(src, ref));
8041ed
-        }
8041ed
-        return matchLen + (zeroBits >>> 3);
8041ed
-      }
8041ed
-    }
8041ed
-    while (sOff < srcLimit && readByte(src, ref++) == readByte(src, sOff++)) {
8041ed
-      ++matchLen;
8041ed
-    }
8041ed
-    return matchLen;
8041ed
-  }
8041ed
-
8041ed
-  static int writeLen(int len, byte[] dest, int dOff) {
8041ed
-    while (len >= 0xFF) {
8041ed
-      writeByte(dest, dOff++, 0xFF);
8041ed
-      len -= 0xFF;
8041ed
-    }
8041ed
-    writeByte(dest, dOff++, len);
8041ed
-    return dOff;
8041ed
-  }
8041ed
-
8041ed
-  static int encodeSequence(byte[] src, int anchor, int matchOff, int matchRef, int matchLen, byte[] dest, int dOff, int destEnd) {
8041ed
-    final int runLen = matchOff - anchor;
8041ed
-    final int tokenOff = dOff++;
8041ed
-    int token;
8041ed
-
8041ed
-    if (runLen >= RUN_MASK) {
8041ed
-      token = (byte) (RUN_MASK << ML_BITS);
8041ed
-      dOff = writeLen(runLen - RUN_MASK, dest, dOff);
8041ed
-    } else {
8041ed
-      token = runLen << ML_BITS;
8041ed
-    }
8041ed
-
8041ed
-    // copy literals
8041ed
-    wildArraycopy(src, anchor, dest, dOff, runLen);
8041ed
-    dOff += runLen;
8041ed
-
8041ed
-    // encode offset
8041ed
-    final int matchDec = matchOff - matchRef;
8041ed
-    dest[dOff++] = (byte) matchDec;
8041ed
-    dest[dOff++] = (byte) (matchDec >>> 8);
8041ed
-
8041ed
-    // encode match len
8041ed
-    matchLen -= 4;
8041ed
-    if (dOff + (1 + LAST_LITERALS) + (matchLen >>> 8) > destEnd) {
8041ed
-      throw new LZ4Exception("maxDestLen is too small");
8041ed
-    }
8041ed
-    if (matchLen >= ML_MASK) {
8041ed
-      token |= ML_MASK;
8041ed
-      dOff = writeLen(matchLen - RUN_MASK, dest, dOff);
8041ed
-    } else {
8041ed
-      token |= matchLen;
8041ed
-    }
8041ed
-
8041ed
-    dest[tokenOff] = (byte) token;
8041ed
-
8041ed
-    return dOff;
8041ed
-  }
8041ed
-
8041ed
-  static int commonBytesBackward(byte[] b, int o1, int o2, int l1, int l2) {
8041ed
-    int count = 0;
8041ed
-    while (o1 > l1 && o2 > l2 && readByte(b, --o1) == readByte(b, --o2)) {
8041ed
-      ++count;
8041ed
-    }
8041ed
-    return count;
8041ed
-  }
8041ed
-
8041ed
-  static int lastLiterals(byte[] src, int sOff, int srcLen, byte[] dest, int dOff, int destEnd) {
8041ed
-    return LZ4SafeUtils.lastLiterals(src, sOff, srcLen, dest, dOff, destEnd);
8041ed
-  }
8041ed
-
8041ed
-}
8041ed
diff --git a/src/java-unsafe/net/jpountz/util/UnsafeUtils.java b/src/java-unsafe/net/jpountz/util/UnsafeUtils.java
8041ed
deleted file mode 100644
8041ed
index 30231ef..0000000
8041ed
--- a/src/java-unsafe/net/jpountz/util/UnsafeUtils.java
8041ed
+++ /dev/null
8041ed
@@ -1,147 +0,0 @@
8041ed
-package net.jpountz.util;
8041ed
-
8041ed
-/*
8041ed
- * Licensed under the Apache License, Version 2.0 (the "License");
8041ed
- * you may not use this file except in compliance with the License.
8041ed
- * You may obtain a copy of the License at
8041ed
- *
8041ed
- *     http://www.apache.org/licenses/LICENSE-2.0
8041ed
- *
8041ed
- * Unless required by applicable law or agreed to in writing, software
8041ed
- * distributed under the License is distributed on an "AS IS" BASIS,
8041ed
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
8041ed
- * See the License for the specific language governing permissions and
8041ed
- * limitations under the License.
8041ed
- */
8041ed
-
8041ed
-import static net.jpountz.util.Utils.NATIVE_BYTE_ORDER;
8041ed
-
8041ed
-import java.lang.reflect.Field;
8041ed
-import java.nio.ByteOrder;
8041ed
-
8041ed
-import sun.misc.Unsafe;
8041ed
-
8041ed
-public enum UnsafeUtils {
8041ed
-  ;
8041ed
-
8041ed
-  private static final Unsafe UNSAFE;
8041ed
-  private static final long BYTE_ARRAY_OFFSET;
8041ed
-  private static final int BYTE_ARRAY_SCALE;
8041ed
-  private static final long INT_ARRAY_OFFSET;
8041ed
-  private static final int INT_ARRAY_SCALE;
8041ed
-  private static final long SHORT_ARRAY_OFFSET;
8041ed
-  private static final int SHORT_ARRAY_SCALE;
8041ed
-  
8041ed
-  static {
8041ed
-    try {
8041ed
-      Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
8041ed
-      theUnsafe.setAccessible(true);
8041ed
-      UNSAFE = (Unsafe) theUnsafe.get(null);
8041ed
-      BYTE_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(byte[].class);
8041ed
-      BYTE_ARRAY_SCALE = UNSAFE.arrayIndexScale(byte[].class);
8041ed
-      INT_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(int[].class);
8041ed
-      INT_ARRAY_SCALE = UNSAFE.arrayIndexScale(int[].class);
8041ed
-      SHORT_ARRAY_OFFSET = UNSAFE.arrayBaseOffset(short[].class);
8041ed
-      SHORT_ARRAY_SCALE = UNSAFE.arrayIndexScale(short[].class);
8041ed
-    } catch (IllegalAccessException e) {
8041ed
-      throw new ExceptionInInitializerError("Cannot access Unsafe");
8041ed
-    } catch (NoSuchFieldException e) {
8041ed
-      throw new ExceptionInInitializerError("Cannot access Unsafe");
8041ed
-    } catch (SecurityException e) {
8041ed
-      throw new ExceptionInInitializerError("Cannot access Unsafe");
8041ed
-    }
8041ed
-  }
8041ed
-
8041ed
-  public static void checkRange(byte[] buf, int off) {
8041ed
-    SafeUtils.checkRange(buf, off);
8041ed
-  }
8041ed
-
8041ed
-  public static void checkRange(byte[] buf, int off, int len) {
8041ed
-    SafeUtils.checkRange(buf, off, len);
8041ed
-  }
8041ed
-
8041ed
-  public static void checkLength(int len) {
8041ed
-    SafeUtils.checkLength(len);
8041ed
-  }
8041ed
-
8041ed
-  public static byte readByte(byte[] src, int srcOff) {
8041ed
-    return UNSAFE.getByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff);
8041ed
-  }
8041ed
-
8041ed
-  public static void writeByte(byte[] src, int srcOff, byte value) {
8041ed
-    UNSAFE.putByte(src, BYTE_ARRAY_OFFSET + BYTE_ARRAY_SCALE * srcOff, (byte) value);
8041ed
-  }
8041ed
-
8041ed
-  public static void writeByte(byte[] src, int srcOff, int value) {
8041ed
-    writeByte(src, srcOff, (byte) value);
8041ed
-  }
8041ed
-
8041ed
-  public static long readLong(byte[] src, int srcOff) {
8041ed
-    return UNSAFE.getLong(src, BYTE_ARRAY_OFFSET + srcOff);
8041ed
-  }
8041ed
-
8041ed
-  public static long readLongLE(byte[] src, int srcOff) {
8041ed
-    long i = readLong(src, srcOff);
8041ed
-    if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-      i = Long.reverseBytes(i);
8041ed
-    }
8041ed
-    return i;
8041ed
-  }
8041ed
-
8041ed
-  public static void writeLong(byte[] dest, int destOff, long value) {
8041ed
-    UNSAFE.putLong(dest, BYTE_ARRAY_OFFSET + destOff, value);
8041ed
-  }
8041ed
-
8041ed
-  public static int readInt(byte[] src, int srcOff) {
8041ed
-    return UNSAFE.getInt(src, BYTE_ARRAY_OFFSET + srcOff);
8041ed
-  }
8041ed
-
8041ed
-  public static int readIntLE(byte[] src, int srcOff) {
8041ed
-    int i = readInt(src, srcOff);
8041ed
-    if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-      i = Integer.reverseBytes(i);
8041ed
-    }
8041ed
-    return i;
8041ed
-  }
8041ed
-
8041ed
-  public static void writeInt(byte[] dest, int destOff, int value) {
8041ed
-    UNSAFE.putInt(dest, BYTE_ARRAY_OFFSET + destOff, value);
8041ed
-  }
8041ed
-
8041ed
-  public static short readShort(byte[] src, int srcOff) {
8041ed
-    return UNSAFE.getShort(src, BYTE_ARRAY_OFFSET + srcOff);
8041ed
-  }
8041ed
-
8041ed
-  public static int readShortLE(byte[] src, int srcOff) {
8041ed
-    short s = readShort(src, srcOff);
8041ed
-    if (NATIVE_BYTE_ORDER == ByteOrder.BIG_ENDIAN) {
8041ed
-      s = Short.reverseBytes(s);
8041ed
-    }
8041ed
-    return s & 0xFFFF;
8041ed
-  }
8041ed
-
8041ed
-  public static void writeShort(byte[] dest, int destOff, short value) {
8041ed
-    UNSAFE.putShort(dest, BYTE_ARRAY_OFFSET + destOff, value);
8041ed
-  }
8041ed
-
8041ed
-  public static void writeShortLE(byte[] buf, int off, int v) {
8041ed
-    writeByte(buf, off, (byte) v);
8041ed
-    writeByte(buf, off + 1, (byte) (v >>> 8));
8041ed
-  }
8041ed
-
8041ed
-  public static int readInt(int[] src, int srcOff) {
8041ed
-    return UNSAFE.getInt(src, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * srcOff);
8041ed
-  }
8041ed
-
8041ed
-  public static void writeInt(int[] dest, int destOff, int value) {
8041ed
-    UNSAFE.putInt(dest, INT_ARRAY_OFFSET + INT_ARRAY_SCALE * destOff, value);
8041ed
-  }
8041ed
-
8041ed
-  public static int readShort(short[] src, int srcOff) {
8041ed
-    return UNSAFE.getShort(src, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * srcOff) & 0xFFFF;
8041ed
-  }
8041ed
-
8041ed
-  public static void writeShort(short[] dest, int destOff, int value) {
8041ed
-    UNSAFE.putShort(dest, SHORT_ARRAY_OFFSET + SHORT_ARRAY_SCALE * destOff, (short) value);
8041ed
-  }
8041ed
-}
8041ed
diff --git a/src/java/net/jpountz/lz4/LZ4Factory.java b/src/java/net/jpountz/lz4/LZ4Factory.java
8041ed
index e04867b..69f81fa 100644
8041ed
--- a/src/java/net/jpountz/lz4/LZ4Factory.java
8041ed
+++ b/src/java/net/jpountz/lz4/LZ4Factory.java
8041ed
@@ -32,8 +32,6 @@ import static net.jpountz.lz4.LZ4Constants.MAX_COMPRESSION_LEVEL;
8041ed
  * the original LZ4 C implementation.
8041ed
  * 
  • a {@link #safeInstance() safe Java} instance which is a pure Java port
  • 8041ed
      * of the original C library,
    8041ed
    - * 
  • an {@link #unsafeInstance() unsafe Java} instance which is a Java port
  • 8041ed
    - * using the unofficial {@link sun.misc.Unsafe} API.
    8041ed
      * 
    8041ed
      * 

    8041ed
      * Only the {@link #safeInstance() safe instance} is guaranteed to work on your
    8041ed
    @@ -55,7 +53,6 @@ public final class LZ4Factory {
    8041ed
       }
    8041ed
     
    8041ed
       private static LZ4Factory NATIVE_INSTANCE,
    8041ed
    -                            JAVA_UNSAFE_INSTANCE,
    8041ed
                                 JAVA_SAFE_INSTANCE;
    8041ed
     
    8041ed
       /**
    8041ed
    @@ -107,42 +104,15 @@ public final class LZ4Factory {
    8041ed
         return JAVA_SAFE_INSTANCE;
    8041ed
       }
    8041ed
     
    8041ed
    -  /**
    8041ed
    -   * Returns a {@link LZ4Factory} instance that returns compressors and
    8041ed
    -   * decompressors that may use {@link sun.misc.Unsafe} to speed up compression
    8041ed
    -   * and decompression.
    8041ed
    -   *
    8041ed
    -   * @return a {@link LZ4Factory} instance that returns compressors and
    8041ed
    -   * decompressors that may use {@link sun.misc.Unsafe} to speed up compression
    8041ed
    -   * and decompression.
    8041ed
    -   */
    8041ed
    -  public static synchronized LZ4Factory unsafeInstance() {
    8041ed
    -    if (JAVA_UNSAFE_INSTANCE == null) {
    8041ed
    -      JAVA_UNSAFE_INSTANCE = instance("JavaUnsafe");
    8041ed
    -    }
    8041ed
    -    return JAVA_UNSAFE_INSTANCE;
    8041ed
    -  }
    8041ed
    -
    8041ed
       /**
    8041ed
        * Returns the fastest available {@link LZ4Factory} instance which does not
    8041ed
    -   * rely on JNI bindings. It first tries to load the
    8041ed
    -   * {@link #unsafeInstance() unsafe instance}, and then the
    8041ed
    -   * {@link #safeInstance() safe Java instance} if the JVM doesn't have a
    8041ed
    -   * working {@link sun.misc.Unsafe}.
    8041ed
    +   * rely on JNI bindings. It loads {@link #safeInstance() safe Java instance}
    8041ed
        *
    8041ed
        * @return the fastest available {@link LZ4Factory} instance which does not
    8041ed
        * rely on JNI bindings.
    8041ed
        */
    8041ed
       public static LZ4Factory fastestJavaInstance() {
    8041ed
    -    if (Utils.isUnalignedAccessAllowed()) {
    8041ed
    -      try {
    8041ed
    -        return unsafeInstance();
    8041ed
    -      } catch (Throwable t) {
    8041ed
    -        return safeInstance();
    8041ed
    -      }
    8041ed
    -    } else {
    8041ed
           return safeInstance();
    8041ed
    -    }
    8041ed
       }
    8041ed
     
    8041ed
       /**
    8041ed
    @@ -249,9 +219,9 @@ public final class LZ4Factory {
    8041ed
        *   
  • A compression level lower than 1 would be treated as 9.
  • 8041ed
        * 
    8041ed
        * Note that compression levels from different implementations
    8041ed
    -   * (native, unsafe Java, and safe Java) cannot be compared with one another.
    8041ed
    +   * (native, and safe Java) cannot be compared with one another.
    8041ed
        * Specifically, the native implementation of a high compression level
    8041ed
    -   * is not necessarily faster than the safe/unsafe Java implementation
    8041ed
    +   * is not necessarily faster than the safe Java implementation
    8041ed
        * of the same compression level.
    8041ed
        *
    8041ed
        * @param compressionLevel the compression level between [1, 17]; the higher the level, the higher the compression ratio
    8041ed
    diff --git a/src/java/net/jpountz/xxhash/XXHashFactory.java b/src/java/net/jpountz/xxhash/XXHashFactory.java
    8041ed
    index 9a9b4d1..9ccdf76 100644
    8041ed
    --- a/src/java/net/jpountz/xxhash/XXHashFactory.java
    8041ed
    +++ b/src/java/net/jpountz/xxhash/XXHashFactory.java
    8041ed
    @@ -28,8 +28,6 @@ import net.jpountz.util.Utils;
    8041ed
      * the original LZ4 C implementation.
    8041ed
      * 
  • a {@link #safeInstance() safe Java} instance which is a pure Java port
  • 8041ed
      * of the original C library,
    8041ed
    - * 
  • an {@link #unsafeInstance() unsafe Java} instance which is a Java port
  • 8041ed
    - * using the unofficial {@link sun.misc.Unsafe} API.
    8041ed
      * 
    8041ed
      * 

    8041ed
      * Only the {@link #safeInstance() safe instance} is guaranteed to work on your
    8041ed
    @@ -51,7 +49,6 @@ public final class XXHashFactory {
    8041ed
       }
    8041ed
     
    8041ed
       private static XXHashFactory NATIVE_INSTANCE,
    8041ed
    -                               JAVA_UNSAFE_INSTANCE,
    8041ed
                                    JAVA_SAFE_INSTANCE;
    8041ed
     
    8041ed
       /**
    8041ed
    @@ -96,40 +93,15 @@ public final class XXHashFactory {
    8041ed
         return JAVA_SAFE_INSTANCE;
    8041ed
       }
    8041ed
     
    8041ed
    -  /**
    8041ed
    -   * Returns a {@link XXHashFactory} that returns {@link XXHash32} instances that
    8041ed
    -   *  may use {@link sun.misc.Unsafe} to speed up hashing.
    8041ed
    -   *
    8041ed
    -   * @return a {@link XXHashFactory} that returns {@link XXHash32} instances that
    8041ed
    -   *  may use {@link sun.misc.Unsafe} to speed up hashing.
    8041ed
    -   */
    8041ed
    -  public static synchronized XXHashFactory unsafeInstance() {
    8041ed
    -    if (JAVA_UNSAFE_INSTANCE == null) {
    8041ed
    -      JAVA_UNSAFE_INSTANCE = instance("JavaUnsafe");
    8041ed
    -    }
    8041ed
    -    return JAVA_UNSAFE_INSTANCE;
    8041ed
    -  }
    8041ed
    -
    8041ed
       /**
    8041ed
        * Returns the fastest available {@link XXHashFactory} instance which does not
    8041ed
    -   * rely on JNI bindings. It first tries to load the
    8041ed
    -   * {@link #unsafeInstance() unsafe instance}, and then the
    8041ed
    -   * {@link #safeInstance() safe Java instance} if the JVM doesn't have a
    8041ed
    -   * working {@link sun.misc.Unsafe}.
    8041ed
    +   * rely on JNI bindings. It loads {@link #safeInstance() safe instance}
    8041ed
        *
    8041ed
        * @return the fastest available {@link XXHashFactory} instance which does not
    8041ed
        * rely on JNI bindings.
    8041ed
        */
    8041ed
       public static XXHashFactory fastestJavaInstance() {
    8041ed
    -    if (Utils.isUnalignedAccessAllowed()) {
    8041ed
    -      try {
    8041ed
    -        return unsafeInstance();
    8041ed
    -      } catch (Throwable t) {
    8041ed
    -        return safeInstance();
    8041ed
    -      }
    8041ed
    -    } else {
    8041ed
           return safeInstance();
    8041ed
    -    }
    8041ed
       }
    8041ed
     
    8041ed
       /**
    8041ed
    diff --git a/src/test/net/jpountz/lz4/Instances.java b/src/test/net/jpountz/lz4/Instances.java
    8041ed
    index b9caae5..44f7809 100644
    8041ed
    --- a/src/test/net/jpountz/lz4/Instances.java
    8041ed
    +++ b/src/test/net/jpountz/lz4/Instances.java
    8041ed
    @@ -21,21 +21,17 @@ enum Instances {
    8041ed
       static LZ4Compressor[] COMPRESSORS = new LZ4Compressor[] {
    8041ed
         LZ4Factory.nativeInstance().fastCompressor(),
    8041ed
         LZ4Factory.nativeInstance().highCompressor(),
    8041ed
    -    LZ4Factory.unsafeInstance().fastCompressor(),
    8041ed
    -    LZ4Factory.unsafeInstance().highCompressor(),
    8041ed
         LZ4Factory.safeInstance().fastCompressor(),
    8041ed
         LZ4Factory.safeInstance().highCompressor()
    8041ed
       };
    8041ed
     
    8041ed
       static LZ4FastDecompressor[] FAST_DECOMPRESSORS = new LZ4FastDecompressor[] {
    8041ed
         LZ4Factory.nativeInstance().fastDecompressor(),
    8041ed
    -    LZ4Factory.unsafeInstance().fastDecompressor(),
    8041ed
         LZ4Factory.safeInstance().fastDecompressor()
    8041ed
       };
    8041ed
     
    8041ed
       static LZ4SafeDecompressor[] SAFE_DECOMPRESSORS = new LZ4SafeDecompressor[] {
    8041ed
         LZ4Factory.nativeInstance().safeDecompressor(),
    8041ed
    -    LZ4Factory.unsafeInstance().safeDecompressor(),
    8041ed
         LZ4Factory.safeInstance().safeDecompressor()
    8041ed
       };
    8041ed
     
    8041ed
    diff --git a/src/test/net/jpountz/lz4/LZ4FactoryTest.java b/src/test/net/jpountz/lz4/LZ4FactoryTest.java
    8041ed
    index c4ef05e..b8b33d7 100644
    8041ed
    --- a/src/test/net/jpountz/lz4/LZ4FactoryTest.java
    8041ed
    +++ b/src/test/net/jpountz/lz4/LZ4FactoryTest.java
    8041ed
    @@ -21,17 +21,13 @@ public class LZ4FactoryTest extends TestCase {
    8041ed
       public void test() {
    8041ed
         assertEquals(LZ4JNICompressor.INSTANCE, LZ4Factory.nativeInstance().fastCompressor());
    8041ed
         assertEquals(LZ4HCJNICompressor.INSTANCE, LZ4Factory.nativeInstance().highCompressor());
    8041ed
    -    assertEquals(LZ4JavaUnsafeCompressor.INSTANCE, LZ4Factory.unsafeInstance().fastCompressor());
    8041ed
    -    assertEquals(LZ4HCJavaUnsafeCompressor.INSTANCE, LZ4Factory.unsafeInstance().highCompressor());
    8041ed
         assertEquals(LZ4JavaSafeCompressor.INSTANCE, LZ4Factory.safeInstance().fastCompressor());
    8041ed
         assertEquals(LZ4HCJavaSafeCompressor.INSTANCE, LZ4Factory.safeInstance().highCompressor());
    8041ed
     
    8041ed
         assertEquals(LZ4JNIFastDecompressor.INSTANCE, LZ4Factory.nativeInstance().fastDecompressor());
    8041ed
    -    assertEquals(LZ4JavaUnsafeFastDecompressor.INSTANCE, LZ4Factory.unsafeInstance().fastDecompressor());
    8041ed
         assertEquals(LZ4JavaSafeFastDecompressor.INSTANCE, LZ4Factory.safeInstance().fastDecompressor());
    8041ed
     
    8041ed
         assertEquals(LZ4JNISafeDecompressor.INSTANCE, LZ4Factory.nativeInstance().safeDecompressor());
    8041ed
    -    assertEquals(LZ4JavaUnsafeSafeDecompressor.INSTANCE, LZ4Factory.unsafeInstance().safeDecompressor());
    8041ed
         assertEquals(LZ4JavaSafeSafeDecompressor.INSTANCE, LZ4Factory.safeInstance().safeDecompressor());
    8041ed
       }
    8041ed
     
    8041ed
    diff --git a/src/test/net/jpountz/xxhash/XXHashFactoryTest.java b/src/test/net/jpountz/xxhash/XXHashFactoryTest.java
    8041ed
    index c410220..2aae562 100644
    8041ed
    --- a/src/test/net/jpountz/xxhash/XXHashFactoryTest.java
    8041ed
    +++ b/src/test/net/jpountz/xxhash/XXHashFactoryTest.java
    8041ed
    @@ -21,14 +21,10 @@ public class XXHashFactoryTest extends TestCase {
    8041ed
       public void test() {
    8041ed
         assertEquals(XXHash32JNI.INSTANCE, XXHashFactory.nativeInstance().hash32());
    8041ed
         assertTrue(XXHashFactory.nativeInstance().newStreamingHash32(0) instanceof StreamingXXHash32JNI);
    8041ed
    -    assertEquals(XXHash32JavaUnsafe.INSTANCE, XXHashFactory.unsafeInstance().hash32());
    8041ed
    -    assertTrue(XXHashFactory.unsafeInstance().newStreamingHash32(0) instanceof StreamingXXHash32JavaUnsafe);
    8041ed
         assertEquals(XXHash32JavaSafe.INSTANCE, XXHashFactory.safeInstance().hash32());
    8041ed
         assertTrue(XXHashFactory.safeInstance().newStreamingHash32(0) instanceof StreamingXXHash32JavaSafe);
    8041ed
         assertEquals(XXHash64JNI.INSTANCE, XXHashFactory.nativeInstance().hash64());
    8041ed
         assertTrue(XXHashFactory.nativeInstance().newStreamingHash64(0) instanceof StreamingXXHash64JNI);
    8041ed
    -    assertEquals(XXHash64JavaUnsafe.INSTANCE, XXHashFactory.unsafeInstance().hash64());
    8041ed
    -    assertTrue(XXHashFactory.unsafeInstance().newStreamingHash64(0) instanceof StreamingXXHash64JavaUnsafe);
    8041ed
         assertEquals(XXHash64JavaSafe.INSTANCE, XXHashFactory.safeInstance().hash64());
    8041ed
         assertTrue(XXHashFactory.safeInstance().newStreamingHash64(0) instanceof StreamingXXHash64JavaSafe);
    8041ed
       }