How to read a whole binary file (Nippy) into byte array in Clojure?

前端 未结 6 1443
Happy的楠姐
Happy的楠姐 2021-01-12 18:05

I need to convert Nippy data structures stored on disk into something that can be read by Nippy? Nippy uses byte arrays, so I need some way to convert the file into a byte a

6条回答
  •  情话喂你
    2021-01-12 18:46

    Here's how I do it generically with clojure built-ins

    (defn slurp-bytes
      "Slurp the bytes from a slurpable thing"
      [x]
      (with-open [out (java.io.ByteArrayOutputStream.)]
        (clojure.java.io/copy (clojure.java.io/input-stream x) out)
        (.toByteArray out)))
    

提交回复
热议问题