-
-
Save jeroenvandijk/bf5018238dd1aaa69f696c320673910e to your computer and use it in GitHub Desktop.
Manifold body stream to input-stream
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[byte-streams :as bs] | |
'[manifold.deferred :as d] | |
'[manifold.stream :as stream]) | |
(import '[java.io ByteArrayInputStream]) | |
(defn raw-stream->input-stream [byte-stream total-bytes] | |
(let [bs-array (byte-array total-bytes)] | |
(d/chain byte-stream | |
#(stream/map bs/to-byte-array %) | |
#(stream/reduce | |
(fn [offset bytes] | |
(let [length (alength bytes) | |
new-length (+ offset length)] | |
(when (> new-length total-bytes) | |
(throw (ex-info "Reached max bytes" {:total-bytes total-bytes}))) | |
(System/arraycopy bytes 0 bs-array offset length) | |
new-length)) | |
0 | |
%) | |
(fn [length] | |
(ByteArrayInputStream. bs-array 0 length))))) | |
(defn input-stream->string [input-stream] | |
(with-open [in input-stream] | |
(slurp in))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment