Blame SOURCES/0-remove-unsafe.patch

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

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

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