Skip to content

Instantly share code, notes, and snippets.

@angerman
Created December 3, 2009 21:16
Show Gist options
  • Save angerman/248531 to your computer and use it in GitHub Desktop.
Save angerman/248531 to your computer and use it in GitHub Desktop.
(in-ns 'clojure.contrib.duck-streams)
(import '(java.io File RandomAccessFile))
;; copied and adapted from c.c.ds
(defmulti #^{:arglist '([x])} random-access-reader class)
(defmethod random-access-reader File [#^File x]
(RandomAccessFile. x "r"))
(defmethod random-access-reader String [#^String x]
(random-access-reader (file-str x)))
(defmethod random-access-reader :default [x]
(throw (Exception. (str "Cannot open " (pr-str x) " as a reader."))))
;; causes the problem when eveluated on the repl
; (let [raf (random-access-reader "/Users/angerman/upps.data.model")
; line (.readLine raf)]
; nil)
(defmacro with-random-access-reader
"Opens a RandomAccessFile on f, binds it to *file*, and evaluates body."
[f & body]
`(with-open [stream# (random-access-reader ~f)]
(binding [*raf* stream#]
~@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment