Created
March 28, 2012 22:43
-
-
Save samaaron/2231226 to your computer and use it in GitHub Desktop.
binary file slurping and spitting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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