On Thu, Feb 26, 2009 at 3:04 PM, Martin DeMello <martindeme...@gmail.com> wrote:
>
> Is there a quick way to read a file into a java array of bytes?

(let [fl (java.io.File. "/tmp/datafile")
      ary (make-array Byte/TYPE (.length fl))]
  (with-open [strm (java.io.FileInputStream. fl)]
    (.read strm ary 0 (.length fl)))
  ary)

Hm.  Or maybe java.nio?

(with-open [strm (java.io.FileInputStream. "/tmp/datafile")]
  (let [ch (.getChannel strm)
        buf (java.nio.ByteBuffer/allocate (.size ch))]
    (.read ch buf)
    (.array buf)))

Bleh.  Anybody got something better?

(with-open [strm (java.io.FileInputStream. "/tmp/datafile")]
  (let [ch (.getChannel strm)
        buf (.map ch java.nio.channels.FileChannel$MapMode/READ_ONLY 0
(.size ch))
        ary (make-array Byte/TYPE (.size ch))]
    (.get buf ary)
    ary))

Seriously, what's with this API?  I give up.

--Chouser

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to