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


The following commit(s) were added to refs/heads/master by this push:
     new ddf3c22  Reuse Commons IO
ddf3c22 is described below

commit ddf3c224542fcc918be8523fc00ea8e823e630cf
Author: Gary Gregory <garydgreg...@gmail.com>
AuthorDate: Sat Sep 30 10:18:00 2023 -0400

    Reuse Commons IO
---
 pom.xml                                            |  5 ++
 .../apache/commons/crypto/NativeCodeLoader.java    | 63 +---------------------
 2 files changed, 7 insertions(+), 61 deletions(-)

diff --git a/pom.xml b/pom.xml
index b4d0b59..1fb6070 100644
--- a/pom.xml
+++ b/pom.xml
@@ -742,6 +742,11 @@ The following provides more details on the included 
cryptographic software:
       <artifactId>jna</artifactId>
       <version>${jna.version}</version>
     </dependency>
+    <dependency>
+      <groupId>commons-io</groupId>
+      <artifactId>commons-io</artifactId>
+      <version>2.14.0</version>
+    </dependency>
     <!-- testing -->
     <dependency>
       <groupId>org.junit.jupiter</groupId>
diff --git a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java 
b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
index 422685a..fd03dcb 100644
--- a/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
+++ b/src/main/java/org/apache/commons/crypto/NativeCodeLoader.java
@@ -17,7 +17,6 @@
  */
 package org.apache.commons.crypto;
 
-import java.io.BufferedInputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -25,11 +24,11 @@ import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.StandardCopyOption;
 import java.nio.file.attribute.PosixFileAttributes;
-import java.util.Objects;
 import java.util.Properties;
 import java.util.UUID;
 
 import org.apache.commons.crypto.utils.Utils;
+import org.apache.commons.io.IOUtils;
 
 /**
  * A helper to load the native code i.e. libcommons-crypto.so. This handles the
@@ -44,11 +43,6 @@ final class NativeCodeLoader {
 
     private static final String NATIVE_LIBNAME_ALT = "lib" + NATIVE_LIBNAME + 
".jnilib";
 
-    /**
-     * End of file pseudo-character.
-     */
-    private static final int EOF = -1;
-
     private static final Throwable libraryLoadingError;
 
     private static final boolean libraryLoaded;
@@ -61,59 +55,6 @@ final class NativeCodeLoader {
         debug("%s static init end", SIMPLE_NAME);
     }
 
-    /**
-     * Returns the given InputStream if it is already a {@link 
BufferedInputStream},
-     * otherwise creates a BufferedInputStream from the given InputStream.
-     * <p>
-     * Copied from Apache Commons IO 2.5.
-     * </p>
-     *
-     * @param inputStream the InputStream to wrap or return (not {@code null})
-     * @return the given InputStream or a new {@link BufferedInputStream} for 
the
-     *         given InputStream
-     * @throws NullPointerException if the input parameter is {@code null}
-     */
-    @SuppressWarnings("resource")
-    private static BufferedInputStream buffer(final InputStream inputStream) {
-        // reject null early on rather than waiting for IO operation to fail
-        // not checked by BufferedInputStream
-        Objects.requireNonNull(inputStream, "inputStream");
-        return inputStream instanceof BufferedInputStream ? 
(BufferedInputStream) inputStream
-                : new BufferedInputStream(inputStream);
-    }
-
-    /**
-     * Checks whether in1 and in2 is equal.
-     * <p>
-     * Copied from Apache Commons IO 2.5.
-     * </p>
-     *
-     * @param input1 the input1.
-     * @param input2 the input2.
-     * @return {@code true} if in1 and in2 is equal, else {@code false}.
-     * @throws IOException if an I/O error occurs.
-     */
-    @SuppressWarnings("resource")
-    private static boolean contentsEquals(final InputStream input1, final 
InputStream input2) throws IOException {
-        if (input1 == input2) {
-            return true;
-        }
-        if (input1 == null ^ input2 == null) {
-            return false;
-        }
-        final BufferedInputStream bufferedInput1 = buffer(input1);
-        final BufferedInputStream bufferedInput2 = buffer(input2);
-        int ch = bufferedInput1.read();
-        while (EOF != ch) {
-            final int ch2 = bufferedInput2.read();
-            if (ch != ch2) {
-                return false;
-            }
-            ch = bufferedInput1.read();
-        }
-        return bufferedInput2.read() == EOF;
-    }
-
     /**
      * Logs debug messages.
      *
@@ -185,7 +126,7 @@ final class NativeCodeLoader {
             try (InputStream nativeInputStream = 
NativeCodeLoader.class.getResourceAsStream(nativeLibraryFilePath)) {
                 try (InputStream extractedLibIn = Files.newInputStream(path)) {
                     debug("Validating '%s'...", extractedLibFile);
-                    if (!contentsEquals(nativeInputStream, extractedLibIn)) {
+                    if (!IOUtils.contentEquals(nativeInputStream, 
extractedLibIn)) {
                         throw new IllegalStateException(String.format("Failed 
to write a native library file %s to %s",
                                 nativeLibraryFilePath, extractedLibFile));
                     }

Reply via email to