On Jan 7, 7:14 pm, "Brian Doyle" <[email protected]> wrote:
> (defn write-bytes
> "Writes the bytes from the in-stream to the given filename."
> [#^java.io.InputStream in-stream #^String filename]
> (with-open [out-stream (new FileOutputStream filename)]
> (let [buffer (make-array (Byte/TYPE) 4096)]
> (loop [bytes (.read in-stream buffer)]
> (if (not (neg? bytes))
> (do
> (.write out-stream buffer 0 bytes)
> (recur (.read in-stream buffer))))))))
Might I suggest that write-bytes be renamed write-stream, or even spit-
stream? Also it would be useful if it was divided into two functions:
(defn pipe-stream
"Pipe the contents of an InputStream into an OutputStream."
([in out]
(pipe-stream in out 4096))
([#^InputStream in #^OutputStream out bufsize]
(let [buffer (make-array Byte/TYPE bufsize)]
(loop [len (.read in buffer)]
(when (pos? len)
(.write out buffer 0 len)
(recur (.read in buffer)))))))
(def write-stream
"Write the contents of an InputStream to a file."
[in filename]
(pipe-stream in (new FileOutputStream filename)))
- James
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---