# HG changeset patch
# User Anton Vodonosov <avodonosov@yandex.ru>
# Date 1591183464 -10800
#      Wed Jun 03 14:24:24 2020 +0300
# Node ID fa084b0f7b141aad916af88f1dbc24dbbe1153a5
# Parent  e997fa4665748951d229e8622102c67fca5f22f9
# Parent  ed980cfac3a263beb45a189d5e546df84f24011e

abcl: fix accessing underlying fd with openjdk8 and openjdk14

Merged as <https://github.com/cl-plus-ssl/cl-plus-ssl/pull/103>

diff -r e997fa466574 -r fa084b0f7b14 src/streams.lisp
--- a/src/streams.lisp	Wed May 27 17:20:38 2020 +0300
+++ b/src/streams.lisp	Wed Jun 03 14:24:24 2020 +0300
@@ -493,18 +493,26 @@
   (require :jss)
 
   ;;; N.b. Getting the file descriptor from a socket is not supported
-  ;;; by any published JVM API, but the following private data
-  ;;; structures have been present from openjdk6 through openjdk14 so
-  ;;; there's hope that this is somewhat unofficially official.
+  ;;; by any published JVM API, so every JVM implementation may behave
+  ;;; somewhat differently.  By using the ability of
+  ;;; jss:get-java-fields to access private fields, it is usually
+  ;;; possible to "find" an access path to read the underlying integer
+  ;;; value of the file decriptor, which is all we need to pass to
+  ;;; SSL.
   (defmethod stream-fd ((stream system::socket-stream))
     (flet ((get-java-fields (object fields) ;; Thanks to Cyrus Harmon
              (reduce (lambda (x y)
                        (jss:get-java-field x y t))
                      fields
-                     :initial-value object)))
+                     :initial-value object))
+           (jvm-version ()
+             (read
+              (make-string-input-stream
+               (java:jstatic "getProperty" "java.lang.System"
+                                          "java.specification.version")))))
       (ignore-errors
        (get-java-fields (java:jcall "getWrappedInputStream"  ;; TODO: define this as a constant
                                     (two-way-stream-input-stream stream))
-                        '("in" "ch" "fdVal"))))))
-
-
+                        (if (< (jvm-version) 14)
+                            '("in" "ch" "fdVal")
+                            '("in" "this$0" "sc" "fd" "fd")))))))
