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-io.git


The following commit(s) were added to refs/heads/master by this push:
     new dbf443b  [IO-510] Add and adapt ReadAheadInputStream and 
BufferedFileChannelInputStream from Apache Spark.
dbf443b is described below

commit dbf443bc6ad4d546ba7f1dfc051aaebd70ef10f5
Author: Gary Gregory <gardgreg...@gmail.com>
AuthorDate: Thu Oct 29 20:32:35 2020 -0400

    [IO-510] Add and adapt ReadAheadInputStream and
    BufferedFileChannelInputStream from Apache Spark.
    
    Compiles on Java 8 and 11.
---
 .../io/input/BufferedFileChannelInputStream.java        | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java 
b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
index e24c1a0..c0c013c 100644
--- 
a/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
+++ 
b/src/main/java/org/apache/commons/io/input/BufferedFileChannelInputStream.java
@@ -26,7 +26,6 @@ import java.util.Objects;
 
 import org.apache.commons.io.IOUtils;
 
-import sun.misc.Cleaner;
 import sun.nio.ch.DirectBuffer;
 
 /**
@@ -129,13 +128,19 @@ public final class BufferedFileChannelInputStream extends 
InputStream {
         //
         final String specVer = 
System.getProperty("java.specification.version");
         if ("1.8".equals(specVer)) {
-            // On Java 8.
-            final Cleaner cleaner = buffer.cleaner();
-            if (cleaner != null) {
-                cleaner.clean();
+            // On Java 8, but also compiles on Java 11.
+            try {
+              final Class<?> cls = Class.forName("sun.misc.Cleaner");
+              final Object cleaner = buffer.cleaner();
+              if (cleaner != null) {
+                  final Method cleanMethod = cls.getMethod("clean");
+                  cleanMethod.invoke(cleaner);
+              }
+            } catch (ReflectiveOperationException e) {
+                throw new IllegalStateException(e);
             }
         } else {
-            // On Java 9 and up, but compiled on Java 8.
+            // On Java 9 and up, but compiles on Java 8.
             try {
                 final Class<?> cls = Class.forName("sun.misc.Unsafe");
                 final Method cleanerMethod = cls.getMethod("invokeCleaner", 
ByteBuffer.class);

Reply via email to