You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/08/23 14:45:08 UTC

[commons-crypto] 01/02: Sort methods in AB order.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-crypto.git

commit 0ecd5fec80f63b6e533fed9f0c9763b7cfdee621
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Aug 23 10:10:39 2020 -0400

    Sort methods in AB order.
---
 .../apache/commons/crypto/NativeCodeLoader.java    | 202 ++++++++++-----------
 1 file changed, 101 insertions(+), 101 deletions(-)

diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
index dcc1044..6c76a8d 100644
--- a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
+++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
@@ -36,15 +36,9 @@ import org.apache.commons.crypto.utils.Utils;
  */
 final class NativeCodeLoader {
 
-    private final static boolean nativeCodeLoaded;
-
     private static final Throwable loadingError;
 
-    /**
-     * The private constructor of {@link NativeCodeLoader}.
-     */
-    private NativeCodeLoader() {
-    }
+    private final static boolean nativeCodeLoaded;
 
     static {
         loadingError = loadLibrary(); // will be null if loaded OK
@@ -53,80 +47,35 @@ final class NativeCodeLoader {
     }
 
     /**
-     * Loads the library if possible.
+     * Checks whether in1 and in2 is equal.
      *
-     * @return null if successful, otherwise the Throwable that was caught
+     * @param in1 the input1.
+     * @param in2 the input2.
+     * @return true if in1 and in2 is equal, else false.
+     * @throws IOException if an I/O error occurs.
      */
-    static Throwable loadLibrary() {
-        try {
-            final File nativeLibFile = findNativeLibrary();
-            if (nativeLibFile != null) {
-                // Load extracted or specified native library.
-                System.load(nativeLibFile.getAbsolutePath());
-            } else {
-                // Load preinstalled library (in the path -Djava.library.path)
-                System.loadLibrary("commons-crypto");
+    private static boolean contentsEquals(InputStream in1, InputStream in2)
+            throws IOException {
+        if (!(in1 instanceof BufferedInputStream)) {
+            in1 = new BufferedInputStream(in1);
+        }
+        if (!(in2 instanceof BufferedInputStream)) {
+            in2 = new BufferedInputStream(in2);
+        }
+
+        int ch = in1.read();
+        while (ch != -1) {
+            final int ch2 = in2.read();
+            if (ch != ch2) {
+                return false;
             }
-            return null; // OK
-        } catch (final Exception t) {
-            return t;
-        } catch (final UnsatisfiedLinkError t) {
-            return t;
+            ch = in1.read();
         }
+        final int ch2 = in2.read();
+        return ch2 == -1;
     }
 
     /**
-     * Finds the native library.
-     *
-     * @return the jar file.
-     */
-	private static File findNativeLibrary() {
-		// Get the properties once
-		final Properties props = Utils.getDefaultProperties();
-
-		// Try to load the library in commons-crypto.lib.path */
-		String nativeLibraryPath = props.getProperty(Crypto.LIB_PATH_KEY);
-		String nativeLibraryName = props.getProperty(Crypto.LIB_NAME_KEY);
-
-		// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
-		if (nativeLibraryName == null) {
-			nativeLibraryName = System.mapLibraryName("commons-crypto");
-		}
-		if (nativeLibraryPath != null) {
-			final File nativeLib = new File(nativeLibraryPath, nativeLibraryName);
-			if (nativeLib.exists()) {
-				return nativeLib;
-			}
-		}
-
-		// Load an OS-dependent native library inside a jar file
-		nativeLibraryPath = "/org/apache/commons/crypto/native/" + OsInfo.getNativeLibFolderPathForCurrentOS();
-		boolean hasNativeLib = hasResource(nativeLibraryPath + "/" + nativeLibraryName);
-		if (!hasNativeLib) {
-			final String altName = "libcommons-crypto.jnilib";
-			if (OsInfo.getOSName().equals("Mac") && hasResource(nativeLibraryPath + "/" + altName)) {
-				// Fix for openjdk7 for Mac
-				nativeLibraryName = altName;
-				hasNativeLib = true;
-			}
-		}
-
-		if (!hasNativeLib) {
-			final String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s",
-					OsInfo.getOSName(), OsInfo.getArchName());
-			throw new RuntimeException(errorMessage);
-		}
-
-		// Temporary folder for the native lib. Use the value of
-		// commons-crypto.tempdir or java.io.tmpdir
-		final String tempFolder = new File(
-				props.getProperty(Crypto.LIB_TEMPDIR_KEY, System.getProperty("java.io.tmpdir"))).getAbsolutePath();
-
-		// Extract and load a native library inside the jar file
-		return extractLibraryFile(nativeLibraryPath, nativeLibraryName, tempFolder);
-	}
-
-    /**
      * Extracts the specified library file to the target folder.
      *
      * @param libFolderForCurrentOS the library in commons-crypto.lib.path.
@@ -201,32 +150,63 @@ final class NativeCodeLoader {
     }
 
     /**
-     * Checks whether in1 and in2 is equal.
+     * Finds the native library.
      *
-     * @param in1 the input1.
-     * @param in2 the input2.
-     * @return true if in1 and in2 is equal, else false.
-     * @throws IOException if an I/O error occurs.
+     * @return the jar file.
      */
-    private static boolean contentsEquals(InputStream in1, InputStream in2)
-            throws IOException {
-        if (!(in1 instanceof BufferedInputStream)) {
-            in1 = new BufferedInputStream(in1);
-        }
-        if (!(in2 instanceof BufferedInputStream)) {
-            in2 = new BufferedInputStream(in2);
-        }
+	private static File findNativeLibrary() {
+		// Get the properties once
+		final Properties props = Utils.getDefaultProperties();
 
-        int ch = in1.read();
-        while (ch != -1) {
-            final int ch2 = in2.read();
-            if (ch != ch2) {
-                return false;
-            }
-            ch = in1.read();
-        }
-        final int ch2 = in2.read();
-        return ch2 == -1;
+		// Try to load the library in commons-crypto.lib.path */
+		String nativeLibraryPath = props.getProperty(Crypto.LIB_PATH_KEY);
+		String nativeLibraryName = props.getProperty(Crypto.LIB_NAME_KEY);
+
+		// Resolve the library file name with a suffix (e.g., dll, .so, etc.)
+		if (nativeLibraryName == null) {
+			nativeLibraryName = System.mapLibraryName("commons-crypto");
+		}
+		if (nativeLibraryPath != null) {
+			final File nativeLib = new File(nativeLibraryPath, nativeLibraryName);
+			if (nativeLib.exists()) {
+				return nativeLib;
+			}
+		}
+
+		// Load an OS-dependent native library inside a jar file
+		nativeLibraryPath = "/org/apache/commons/crypto/native/" + OsInfo.getNativeLibFolderPathForCurrentOS();
+		boolean hasNativeLib = hasResource(nativeLibraryPath + "/" + nativeLibraryName);
+		if (!hasNativeLib) {
+			final String altName = "libcommons-crypto.jnilib";
+			if (OsInfo.getOSName().equals("Mac") && hasResource(nativeLibraryPath + "/" + altName)) {
+				// Fix for openjdk7 for Mac
+				nativeLibraryName = altName;
+				hasNativeLib = true;
+			}
+		}
+
+		if (!hasNativeLib) {
+			final String errorMessage = String.format("no native library is found for os.name=%s and os.arch=%s",
+					OsInfo.getOSName(), OsInfo.getArchName());
+			throw new RuntimeException(errorMessage);
+		}
+
+		// Temporary folder for the native lib. Use the value of
+		// commons-crypto.tempdir or java.io.tmpdir
+		final String tempFolder = new File(
+				props.getProperty(Crypto.LIB_TEMPDIR_KEY, System.getProperty("java.io.tmpdir"))).getAbsolutePath();
+
+		// Extract and load a native library inside the jar file
+		return extractLibraryFile(nativeLibraryPath, nativeLibraryName, tempFolder);
+	}
+
+    /**
+     * Gets the error cause if loading failed.
+     *
+     * @return null, unless loading failed
+     */
+    static Throwable getLoadingError() {
+        return loadingError;
     }
 
     /**
@@ -249,11 +229,31 @@ final class NativeCodeLoader {
     }
 
     /**
-     * Gets the error cause if loading failed.
+     * Loads the library if possible.
      *
-     * @return null, unless loading failed
+     * @return null if successful, otherwise the Throwable that was caught
      */
-    static Throwable getLoadingError() {
-        return loadingError;
+    static Throwable loadLibrary() {
+        try {
+            final File nativeLibFile = findNativeLibrary();
+            if (nativeLibFile != null) {
+                // Load extracted or specified native library.
+                System.load(nativeLibFile.getAbsolutePath());
+            } else {
+                // Load preinstalled library (in the path -Djava.library.path)
+                System.loadLibrary("commons-crypto");
+            }
+            return null; // OK
+        } catch (final Exception t) {
+            return t;
+        } catch (final UnsatisfiedLinkError t) {
+            return t;
+        }
+    }
+
+    /**
+     * The private constructor of {@link NativeCodeLoader}.
+     */
+    private NativeCodeLoader() {
     }
 }