From ded6cc034f7d2a2334dccf6611fcb6a9935e120a Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Mon, 13 Feb 2017 17:35:18 +0100 Subject: [PATCH] [#91] Add convenience utils for freeze/thaw to/from files Suggested by @Engelberg (thanks Mark!). Also seems to be a common question online, e.g.: http://stackoverflow.com/q/23018870 --- src/taoensso/nippy.clj | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index 26f257a..6b10c08 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -2,6 +2,7 @@ "High-performance serialization library for Clojure" {:author "Peter Taoussanis (@ptaoussanis)"} (:require + [clojure.java.io :as jio] [taoensso.encore :as enc :refer [cond*]] [taoensso.nippy [utils :as utils] @@ -1570,6 +1571,42 @@ (inspect-ba (freeze "hello")) (seq (:data-ba (inspect-ba (freeze "hello"))))) +(defn freeze-to-file + "Convenience util: writes `(freeze x freeze-opts)` byte array to + `(clojure.java.io/file file)` and returns the byte array. + + (freeze-to-file \"my-filename.npy\" my-val) => Serialized byte array" + + ([file x ] (freeze-to-file file x nil)) + ([file x freeze-opts] + (let [^bytes ba (freeze x freeze-opts)] + (with-open [out (jio/output-stream (jio/file file))] + (.write out ba)) + ba))) + +(defn thaw-from-file + "Convenience util: returns `(thaw ba thaw-opts)` Clojure value for the + byte array read from `(clojure.java.io/file file)`. + + (thaw-from-file \"my-filename.npy\") => Deserialized Clojure value + + To thaw from a resource on classpath (e.g in Leiningen `resources` dir): + (thaw-from-file (clojure.java.io/resource \"my-resource-name.npy\"))" + + ([file ] (thaw-from-file file nil)) + ([file thaw-opts] + (let [file (jio/file file), + ba (byte-array (.length file))] + (with-open [in (DataInputStream. (jio/input-stream file))] + (.readFully in ba)) + + (thaw ba thaw-opts)))) + +(comment + (freeze-to-file "foo.npy" "hello, world!") + (thaw-from-file "foo.npy") + (thaw-from-file (jio/resource "foo.npy"))) + ;;;; Deprecated (enc/deprecated