Repository: commons-crypto
Updated Branches:
  refs/heads/master 6a0817217 -> c66387b15


CRYPTO-87 Reduce util package API footprint

freeDirectBuffer only used by Stream classes

Project: http://git-wip-us.apache.org/repos/asf/commons-crypto/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-crypto/commit/c66387b1
Tree: http://git-wip-us.apache.org/repos/asf/commons-crypto/tree/c66387b1
Diff: http://git-wip-us.apache.org/repos/asf/commons-crypto/diff/c66387b1

Branch: refs/heads/master
Commit: c66387b15bec3fd6d554a23148cec78b1f003c82
Parents: 6a08172
Author: Sebb <s...@apache.org>
Authored: Sat Jun 25 13:33:20 2016 +0100
Committer: Sebb <s...@apache.org>
Committed: Sat Jun 25 13:33:20 2016 +0100

----------------------------------------------------------------------
 .../crypto/stream/CryptoInputStream.java        | 35 ++++++++++++++++++--
 .../crypto/stream/CryptoOutputStream.java       |  4 +--
 .../stream/PositionedCryptoInputStream.java     |  2 +-
 .../org/apache/commons/crypto/utils/Utils.java  | 32 ------------------
 4 files changed, 36 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c66387b1/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java 
b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
index f50fe12..9c4b97a 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoInputStream.java
@@ -19,6 +19,7 @@ package org.apache.commons.crypto.stream;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
 import java.nio.channels.ReadableByteChannel;
 import java.security.InvalidAlgorithmParameterException;
@@ -567,8 +568,38 @@ public class CryptoInputStream extends InputStream 
implements
 
     /** Forcibly free the direct buffers. */
     protected void freeBuffers() {
-        Utils.freeDirectBuffer(inBuffer);
-        Utils.freeDirectBuffer(outBuffer);
+        CryptoInputStream.freeDirectBuffer(inBuffer);
+        CryptoInputStream.freeDirectBuffer(outBuffer);
+    }
+
+    /**
+     * Forcibly free the direct buffer.
+     *
+     * @param buffer the bytebuffer to be freed.
+     */
+    static void freeDirectBuffer(ByteBuffer buffer) {
+        try {
+            /* Using reflection to implement sun.nio.ch.DirectBuffer.cleaner()
+            .clean(); */
+            final String SUN_CLASS = "sun.nio.ch.DirectBuffer";
+            Class<?>[] interfaces = buffer.getClass().getInterfaces();
+    
+            for (Class<?> clazz : interfaces) {
+                if (clazz.getName().equals(SUN_CLASS)) {
+                    final Object[] NO_PARAM = new Object[0];
+                    /* DirectBuffer#cleaner() */
+                    Method getCleaner = 
Class.forName(SUN_CLASS).getMethod("cleaner");
+                    Object cleaner = getCleaner.invoke(buffer, NO_PARAM);
+                    /* Cleaner#clean() */
+                    Method cleanMethod = 
Class.forName("sun.misc.Cleaner").getMethod("clean");
+                    cleanMethod.invoke(cleaner, NO_PARAM);
+                    return;
+                }
+            }
+        } catch (ReflectiveOperationException e) { // NOPMD
+            // Ignore the Reflection exception.
+    
+        }
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c66387b1/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java 
b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
index e23e548..efa1f95 100644
--- a/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
+++ b/src/main/java/org/apache/commons/crypto/stream/CryptoOutputStream.java
@@ -402,8 +402,8 @@ public class CryptoOutputStream extends OutputStream 
implements
 
     /** Forcibly free the direct buffers. */
     protected void freeBuffers() {
-        Utils.freeDirectBuffer(inBuffer);
-        Utils.freeDirectBuffer(outBuffer);
+        CryptoInputStream.freeDirectBuffer(inBuffer);
+        CryptoInputStream.freeDirectBuffer(outBuffer);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c66387b1/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
----------------------------------------------------------------------
diff --git 
a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
 
b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
index 939c1c9..eddae71 100644
--- 
a/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
+++ 
b/src/main/java/org/apache/commons/crypto/stream/PositionedCryptoInputStream.java
@@ -386,7 +386,7 @@ public class PositionedCryptoInputStream extends 
CTRCryptoInputStream {
     private void cleanBufferPool() {
         ByteBuffer buf;
         while ((buf = bufferPool.poll()) != null) {
-            Utils.freeDirectBuffer(buf);
+            CryptoInputStream.freeDirectBuffer(buf);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/commons-crypto/blob/c66387b1/src/main/java/org/apache/commons/crypto/utils/Utils.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/commons/crypto/utils/Utils.java 
b/src/main/java/org/apache/commons/crypto/utils/Utils.java
index 1aaf14d..3862fda 100644
--- a/src/main/java/org/apache/commons/crypto/utils/Utils.java
+++ b/src/main/java/org/apache/commons/crypto/utils/Utils.java
@@ -19,8 +19,6 @@ package org.apache.commons.crypto.utils;
 
 import java.io.IOException;
 import java.io.InputStream;
-import java.lang.reflect.Method;
-import java.nio.ByteBuffer;
 import java.security.GeneralSecurityException;
 import java.util.ArrayList;
 import java.util.Enumeration;
@@ -86,36 +84,6 @@ public final class Utils {
     }
 
     /**
-     * Forcibly free the direct buffer.
-     *
-     * @param buffer the bytebuffer to be freed.
-     */
-    public static void freeDirectBuffer(ByteBuffer buffer) {
-        try {
-            /* Using reflection to implement sun.nio.ch.DirectBuffer.cleaner()
-            .clean(); */
-            final String SUN_CLASS = "sun.nio.ch.DirectBuffer";
-            Class<?>[] interfaces = buffer.getClass().getInterfaces();
-
-            for (Class<?> clazz : interfaces) {
-                if (clazz.getName().equals(SUN_CLASS)) {
-                    final Object[] NO_PARAM = new Object[0];
-                    /* DirectBuffer#cleaner() */
-                    Method getCleaner = 
Class.forName(SUN_CLASS).getMethod("cleaner");
-                    Object cleaner = getCleaner.invoke(buffer, NO_PARAM);
-                    /* Cleaner#clean() */
-                    Method cleanMethod = 
Class.forName("sun.misc.Cleaner").getMethod("clean");
-                    cleanMethod.invoke(cleaner, NO_PARAM);
-                    return;
-                }
-            }
-        } catch (ReflectiveOperationException e) { // NOPMD
-            // Ignore the Reflection exception.
-
-        }
-    }
-
-    /**
      * <p>
      * This method is only for Counter (CTR) mode. Generally the CryptoCipher
      * calculates the IV and maintain encryption context internally.For example

Reply via email to