Given an input stream I would like to create a lazy sequence of the data in the form of byte-arrays (chunks). Here's my try:
(defn- read-or-nil [stream]
(let [buf (byte-array 2)]
(when-not (= -1 (.read stream buf))
buf)))
(defn byte-chunk-seq [stream]
(cons (read-or-nil stream) (lazy-seq (byte-chunk-seq stream))))
(with-open [rdr (java.io.FileInputStream. "/tmp/wut")]
(take 2 (byte-chunk-seq rdr)))
In the last statement, where I'm testing the code, I get a:
IOException Stream Closed java.io.FileInputStream.readBytes (FileInputStream.java:-2).
If I change the statement to be a take 1
then it returns fine, but that doesn't help me much. Does anyone have any ideas why this wouldn't work?