Skip to content

Instantly share code, notes, and snippets.

@jafingerhut
Created July 8, 2014 15:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jafingerhut/1c61ec6b99a7656895e0 to your computer and use it in GitHub Desktop.
Save jafingerhut/1c61ec6b99a7656895e0 to your computer and use it in GitHub Desktop.
Example of reading java.io.InputStream in Clojure
(ns inputstream.core
(:require [clojure.java.io :as io]))
(defn read-is [^java.io.InputStream is]
(let [bufsize 8192
buf (byte-array bufsize)]
(loop [total-len 0]
(let [n (.read is buf)]
(cond
(pos? n) (do
;; process n bytes in buf here
(println "Read" n " bytes")
(recur (+ total-len n)))
:else total-len))))) ;; or whatever ret value you want
(defn proc-file [fname]
(read-is (io/input-stream fname)))
@arjamizo
Copy link

do you maybe know how can I get sth like this?


(import  '(java.io BufferedReader InputStreamReader PrintWriter))
(println (apply max [7 5]))

(binding 
      [ 
        *in* (BufferedReader. (InputStreamReader. "1\n2\n3\n"))
        ; *out* (PrintWriter. out)
	  ]
	(println (read))
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment