source: trunk/abcl/contrib/abcl-introspect/util.lisp

Last change on this file was 15367, checked in by Mark Evenson, 4 years ago

abcl-introspect: EXT:STREAM-UNIX-FD returns integer file descriptor

The integer can be passed meaningfully by passed to syscalls like
fdopen().

TODO: implement in the core application, current strategy needs JSS to

reliably finad access private member fields

File size: 2.4 KB
Line 
1(in-package :extensions)
2
3;;; TODO: submit upstream patch to <cffi/src/cffi-abcl.lisp> for removal
4(defun write-class (class-bytes pathname)
5  "Write the Java byte[] array CLASS-BYTES to PATHNAME."
6  (with-open-file (stream pathname
7                          :direction :output
8                          :element-type '(unsigned-byte 8))
9    (dotimes (i (java:jarray-length class-bytes))
10      (write-byte (java:jarray-ref class-bytes i) stream))))
11
12(defun read-class (pathname)
13  "Read the file at PATHNAME as a Java byte[] array"
14  (with-open-file (stream pathname
15                          :direction :input
16                          :element-type '(unsigned-byte 8))
17    (let* ((length
18             (file-length stream))
19           (array
20             (make-array length :element-type '(unsigned-byte 8))))
21      (read-sequence array stream :end length)
22      (java:jnew-array-from-array "byte" array))))
23 
24(export '(write-class
25          read-class)
26        :extensions)
27
28;;;  Determining the underlying unix file descriptor depends on
29;;;  navigating private member structures dependent on the hosting
30;;;  JVMs wrapping of native socket. The JAVA package currently does
31;;;  not have a means for such aggressive intropsection, so we add it
32;;;  as a utility here
33;;;
34;;;  TODO test under :msdog
35;;;  TODO Should be in socket.lisp
36(defun stream-unix-fd (stream)
37  "Return the integer of the underlying unix file descriptor for STREAM
38
39Added by ABCL-INTROSPECT."
40  (check-type stream 'system::socket-stream)
41  (flet ((get-java-fields (object fields) ;; Thanks to Cyrus Harmon
42           (reduce (lambda (x y)
43                     (jss:get-java-field x y t))
44                          fields
45                          :initial-value object))
46                (jvm-version ()
47                  (read
48                   (make-string-input-stream
49                    (java:jstatic "getProperty" "java.lang.System"
50                                  "java.specification.version")))))
51           (ignore-errors
52            (get-java-fields (java:jcall "getWrappedInputStream"  ;; TODO: define this as a constant
53                                         (two-way-stream-input-stream stream))
54                             (if (< (jvm-version) 14)
55                                 '("in" "ch" "fdVal")
56                                 '("in" "this$0" "sc" "fd" "fd"))))))
57
58(export '(stream-unix-fd) :extensions)
Note: See TracBrowser for help on using the repository browser.