I incorporated most of James ideas but I don't like the name pipe-stream. (defn write-stream "Writes the data from the istream to the ostream." ([#^java.io.InputStream istream #^java.io.OutputStream ostream #^Integer buffer-size] (let [buffer (make-array (Byte/TYPE) buffer-size)] (loop [bytes (.read istream buffer)] (when (pos? bytes) (.write ostream buffer 0 bytes) (recur (.read istream buffer)))))) ([#^java.io.InputStream istream #^java.io.OutputStream ostream] (write-stream istream ostream 4096)))
(defn write-stream-to-file "Writes the data from the istream to the file." ([#^java.io.InputStream istream #^String filename #^Integer buffer-size] (with-open [ostream (new FileOutputStream filename)] (write-stream istream ostream buffer-size))) ([#^java.io.InputStream istream #^String filename] (write-stream-to-file istream filename 4096))) On Thu, Jan 8, 2009 at 2:28 AM, James Reeves <weavejes...@googlemail.com>wrote: > > On Jan 7, 7:14 pm, "Brian Doyle" <brianpdo...@gmail.com> 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 clojure@googlegroups.com To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en -~----------~----~----~----~------~----~------~--~---