Skip to content

Instantly share code, notes, and snippets.

@angerman
Created December 3, 2009 20:44
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 angerman/248498 to your computer and use it in GitHub Desktop.
Save angerman/248498 to your computer and use it in GitHub Desktop.
(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. x)))
(defmethod random-access-reader :default [x]
(throw (Exception. (str "Cannot open " (pr-str x) " as a reader."))))
(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 [*file* stream#]
~@body)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment