Skip to content

Instantly share code, notes, and snippets.

@samaaron
Created March 28, 2012 22:43
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 samaaron/2231226 to your computer and use it in GitHub Desktop.
Save samaaron/2231226 to your computer and use it in GitHub Desktop.
binary file slurping and spitting
(ns slurper
(:import [java.io FileInputStream FileOutputStream File]))
(defn slurp-binary
[f-name]
(let [f (File. f-name)
fis (FileInputStream. f)
len (.length f)
ba (byte-array len)]
(loop [offset 0]
(when (< offset len)
(let [num-read (.read fis ba offset (- len offset))]
(when (>= num-read 0)
(recur (+ offset num-read))))))
(.close fis)
ba))
(defn spit-binary
[f-name bytes]
(let [f (File. f-name)
fos (FileOutputStream. f)]
(.write fos bytes)
(.close fos)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment