[#137] Add thaw-from-resource
This commit is contained in:
parent
a98b0d8bb1
commit
93755570a5
1 changed files with 25 additions and 17 deletions
|
|
@ -2084,9 +2084,7 @@
|
||||||
(comment (thaw-from-string (freeze-to-string {:a :A :b [:B1 :B2]})))
|
(comment (thaw-from-string (freeze-to-string {:a :A :b [:B1 :B2]})))
|
||||||
|
|
||||||
(defn freeze-to-file
|
(defn freeze-to-file
|
||||||
"Convenience util: like `freeze`, but writes to `(clojure.java.io/file <file>)`
|
"Convenience util: like `freeze`, but writes to `(clojure.java.io/file <file>)`."
|
||||||
and returns the byte array written.
|
|
||||||
See also `thaw-from-file`."
|
|
||||||
([file x ] (freeze-to-file file x nil))
|
([file x ] (freeze-to-file file x nil))
|
||||||
([file x freeze-opts]
|
([file x freeze-opts]
|
||||||
(let [^bytes ba (freeze x freeze-opts)]
|
(let [^bytes ba (freeze x freeze-opts)]
|
||||||
|
|
@ -2095,25 +2093,35 @@
|
||||||
ba)))
|
ba)))
|
||||||
|
|
||||||
(defn thaw-from-file
|
(defn thaw-from-file
|
||||||
"Convenience util: like `thaw`, but reads from `(clojure.java.io/file <file>)`.
|
"Convenience util: like `thaw`, but reads from `(clojure.java.io/file <file>)`."
|
||||||
|
|
||||||
To thaw from a resource on classpath (e.g in Leiningen `resources` dir):
|
|
||||||
(thaw-from-file (clojure.java.io/resource \"my-resource-name.npy\"))
|
|
||||||
|
|
||||||
See also `freeze-to-file`."
|
|
||||||
([file ] (thaw-from-file file nil))
|
([file ] (thaw-from-file file nil))
|
||||||
([file thaw-opts]
|
([file thaw-opts]
|
||||||
(let [file (jio/file file),
|
(let [file (jio/file file)
|
||||||
ba (byte-array (.length file))]
|
frozen-ba
|
||||||
(with-open [in (DataInputStream. (jio/input-stream file))]
|
(let [ba (byte-array (.length file))]
|
||||||
(.readFully in ba))
|
(with-open [in (DataInputStream. (jio/input-stream file))]
|
||||||
|
(.readFully in ba)
|
||||||
|
(do ba)))]
|
||||||
|
|
||||||
(thaw ba thaw-opts))))
|
(thaw frozen-ba thaw-opts))))
|
||||||
|
|
||||||
|
(defn thaw-from-resource
|
||||||
|
"Convenience util: like `thaw`, but reads from `(clojure.java.io/resource <res>)`."
|
||||||
|
([res ] (thaw-from-resource res nil))
|
||||||
|
([res thaw-opts]
|
||||||
|
(let [res (jio/resource res)
|
||||||
|
frozen-ba
|
||||||
|
(with-open [in (jio/input-stream res)
|
||||||
|
out (ByteArrayOutputStream.)]
|
||||||
|
(jio/copy in out)
|
||||||
|
(.toByteArray out))]
|
||||||
|
|
||||||
|
(thaw frozen-ba thaw-opts))))
|
||||||
|
|
||||||
(comment
|
(comment
|
||||||
(freeze-to-file "foo.npy" "hello, world!")
|
(freeze-to-file "resources/foo.npy" "hello, world!")
|
||||||
(thaw-from-file "foo.npy")
|
(thaw-from-file "resources/foo.npy")
|
||||||
(thaw-from-file (jio/resource "foo.npy")))
|
(thaw-from-resource "foo.npy"))
|
||||||
|
|
||||||
;;;; Deprecated
|
;;;; Deprecated
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue