Skip to content

Instantly share code, notes, and snippets.

@ato
Forked from duncanmak/scratch.clj
Created November 20, 2009 09:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ato/239374 to your computer and use it in GitHub Desktop.
Save ato/239374 to your computer and use it in GitHub Desktop.
(defn write [name data]
(let [buf (make-array Byte/TYPE 1024)
dest (FileOutputStream. name)]
(with-open [r (BufferedInputStream. data)
w (BufferedOutputStream. dest)]
(loop [bytes (.read r buf 0 1024)]
(when-not (= bytes -1)
(.write w buf 0 bytes)
(recur (.read r buf 0 1024)))))
(println "Wrote" name)))
(defn download [url]
(let [idx (inc (.lastIndexOf url (int \/)))
name (subs url idx)
conn (.openConnection (URL. url))]
(doto conn
(.connect))
(write name (.getInputStream conn))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment