Further prep for new API, mark freeze-to-bytes and thaw-from-bytes as deprecated

This commit is contained in:
Peter Taoussanis 2013-06-13 01:33:32 +07:00
parent 9734e882bb
commit 613c77b8a1

View file

@ -278,51 +278,29 @@
;;;; Deprecated API ;;;; Deprecated API
(defn- freeze-to-stream-outer ;; TODO Rewrite in :legacy terms
"Serializes x to given output stream." (defn freeze-to-bytes "DEPRECATED: Use `freeze` instead."
([data-output-stream x] ; For <= 1.0.1 compatibility
(freeze-to-stream-outer data-output-stream x true))
([data-output-stream x print-dup?]
(binding [*print-dup* print-dup?] ; For `pr-str`
(freeze-to-stream x data-output-stream))))
(defn freeze-to-bytes
"Serializes x to a byte array and returns the array."
^bytes [x & {:keys [compress? print-dup? password] ^bytes [x & {:keys [compress? print-dup? password]
:or {compress? true :or {compress? true
print-dup? true}}] print-dup? true}}]
(let [ba (ByteArrayOutputStream.) (let [ba (ByteArrayOutputStream.)
stream (DataOutputStream. ba)] stream (DataOutputStream. ba)]
(freeze-to-stream-outer stream x print-dup?) (binding [*print-dup* print-dup?] (freeze-to-stream x stream))
(let [ba (.toByteArray ba) (let [ba (.toByteArray ba)
ba (if compress? (utils/compress-snappy ba) ba) ba (if compress? (utils/compress-snappy ba) ba)
ba (if password (crypto/encrypt-aes128 password ba) ba)] ba (if password (crypto/encrypt-aes128 password ba) ba)]
ba))) ba)))
(defn- thaw-from-stream-outer ;; TODO Rewrite in :legacy terms
"Deserializes an object from given input stream." (defn thaw-from-bytes "DEPRECATED: Use `thaw` instead."
[data-input-stream read-eval?]
(binding [*read-eval* read-eval?]
(let [;; Support older versions of Nippy that wrote a version header
maybe-schema-header (thaw-from-stream data-input-stream)]
(if (and (string? maybe-schema-header)
(.startsWith ^String maybe-schema-header "\u0000~"))
(thaw-from-stream data-input-stream)
maybe-schema-header))))
(defn thaw-from-bytes
"Deserializes an object from given byte array."
[ba & {:keys [compressed? read-eval? password] [ba & {:keys [compressed? read-eval? password]
:or {compressed? true :or {read-eval? false ; For `read-string` injection safety - NB!!!
read-eval? false ; For `read-string` injection safety - NB!!! compressed? true}}]
}}]
(try (try
(-> (let [ba (if password (crypto/decrypt-aes128 password ba) ba) (let [ba (if password (crypto/decrypt-aes128 password ba) ba)
ba (if compressed? (utils/uncompress-snappy ba) ba)] ba (if compressed? (utils/uncompress-snappy ba) ba)
ba) stream (DataInputStream. (ByteArrayInputStream. ba))]
(ByteArrayInputStream.) (binding [*read-eval* read-eval?] (thaw-from-stream stream)))
(DataInputStream.)
(thaw-from-stream-outer read-eval?))
(catch Exception e (catch Exception e
(throw (Exception. (throw (Exception.
(cond password "Thaw failed. Unencrypted data or bad password?" (cond password "Thaw failed. Unencrypted data or bad password?"
@ -331,6 +309,7 @@
e))))) e)))))
(comment (comment
;; Errors
(-> (freeze-to-bytes "my data" :password [:salted "password"]) (-> (freeze-to-bytes "my data" :password [:salted "password"])
(thaw-from-bytes)) (thaw-from-bytes))
(-> (freeze-to-bytes "my data" :compress? true) (-> (freeze-to-bytes "my data" :compress? true)