From 4d24cdca2a48de3ee94cc0a4983fad7cf082762f Mon Sep 17 00:00:00 2001 From: Michiel Borkent Date: Thu, 31 Mar 2022 10:00:16 +0200 Subject: [PATCH] Proxy support for PipedInputStream and PipedOutputStream * Spire requires proxy support for java.io.PipedInputStream and java.io.PipedOutputStream * proxy support for java.io.PipedInputStream and java.io.PipedOutputStream * proxy tests for PipedInputStream and PipedOutputStream Co-authored-by: Crispin Wellington --- CHANGELOG.md | 4 +++ src/babashka/impl/classes.clj | 1 + src/babashka/impl/proxy.clj | 22 +++++++++++++++- test/babashka/proxy_test.clj | 48 +++++++++++++++++++++++++++++++++++ 4 files changed, 74 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e787834..3f3619a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,10 @@ A preview of the next release can be installed from - [#1216](https://github.com/babashka/babashka/issues/1216): support `core.async/alts!` using polyfill - [#1220](https://github.com/babashka/babashka/issues/1220): add reflection on java.util.concurrent.Future +## 0.7.9-SNAPSHOT + +- [#1224](https://github.com/babashka/babashka/issues/1224): add `proxy` support for `java.io.PipedInputStream` and `java.io.PipedOutputStream`. Add reflection for `java.utils.Scanner`. + ## 0.7.8 (2022-03-13) This release improves compatibility with several libraries: [loom](https://github.com/aysylu/loom), [hugsql.core](https://www.hugsql.org) and [specter](https://github.com/redplanetlabs/specter)! diff --git a/src/babashka/impl/classes.clj b/src/babashka/impl/classes.clj index 892c7623..fe84adb1 100644 --- a/src/babashka/impl/classes.clj +++ b/src/babashka/impl/classes.clj @@ -383,6 +383,7 @@ java.util.NoSuchElementException java.util.Optional java.util.Properties + ;; java.util.Scanner java.util.Set java.util.StringTokenizer java.util.UUID diff --git a/src/babashka/impl/proxy.clj b/src/babashka/impl/proxy.clj index b4341d06..1c0018d4 100644 --- a/src/babashka/impl/proxy.clj +++ b/src/babashka/impl/proxy.clj @@ -72,7 +72,27 @@ ["javax.net.ssl.HostnameVerifier" #{}] (proxy [javax.net.ssl.HostnameVerifier] [] - (verify [host-name session] ((method-or-bust methods 'verify) this host-name session)))))) + (verify [host-name session] ((method-or-bust methods 'verify) this host-name session))) + + ["java.io.PipedInputStream" #{}] + (proxy [java.io.PipedInputStream] [] + (available [] ((method-or-bust methods 'available) this)) + (close [] ((method-or-bust methods 'close) this)) + (read + ([] + ((method-or-bust methods 'read) this)) + ([b off len] + ((method-or-bust methods 'read) this b off len))) + (receive [b] ((method-or-bust methods 'receive) this b))) + + ["java.io.PipedOutputStream" #{}] + (proxy [java.io.PipedOutputStream] [] + (close [] ((method-or-bust methods 'close) this)) + (connect [snk] ((method-or-bust methods 'connect) this snk)) + (flush [] ((method-or-bust methods 'flush) this)) + (write + ([b] ((method-or-bust methods 'write) this b)) + ([b off len] ((method-or-bust methods 'write) this b off len))))))) (defn class-sym [c] (symbol (class-name c))) diff --git a/test/babashka/proxy_test.clj b/test/babashka/proxy_test.clj index 496f6449..109f15e0 100644 --- a/test/babashka/proxy_test.clj +++ b/test/babashka/proxy_test.clj @@ -78,3 +78,51 @@ true false] (bb (with-out-str (clojure.pprint/pprint code)))))) + +(deftest PipedInputStream-PipedOutputStream-proxy-test + (is (= {:available 1 + :read-result -1 + :byte-read 10 + :array-read '(0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 0) + :instance? true} + (bb (with-out-str + (clojure.pprint/pprint + '(let [ins (proxy [java.io.PipedInputStream] [] + (available [] 1) + (close [] nil) + (read + ([] 10) + ([byte-arr off len] + (aset byte-arr off (byte 10)) + -1)) + (receive [b] + nil)) + arr (byte-array 16) + ] + {:available (.available ins) + :read-result (.read ins arr 6 2) + :byte-read (.read ins) + :array-read (seq arr) + :instance? (instance? java.io.PipedInputStream ins)})))))) + + (is (= {:instance? true + :arr '(1 2 3 4 5 0 0 0) + :arr2 '(10)} + (bb (with-out-str + (clojure.pprint/pprint + '(let [arr (byte-array 8) + arr2 (byte-array 1) + outs (proxy [java.io.PipedOutputStream] [] + (close [] nil) + (connect [sink] nil) + (flush [] nil) + (write + ([b] (aset arr2 0 (byte b))) + ([byte-arr off len] + (doseq [n (range len)] + (aset arr n (aget byte-arr (+ off n)))))))] + (.write outs (int 10)) + (.write outs (byte-array [0 0 0 1 2 3 4 5 0 0 0]) 3 5) + {:instance? (instance? java.io.PipedOutputStream outs) + :arr (seq arr) + :arr2 (seq arr2)})))))))