From 26ad8dd2529bb9de29cec3d17fdd5e8a326ab525 Mon Sep 17 00:00:00 2001 From: Peter Taoussanis Date: Sun, 16 Jun 2013 12:51:30 +0700 Subject: [PATCH] Reorganize low-level stream fns (prepping for perf work) --- src/taoensso/nippy.clj | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/taoensso/nippy.clj b/src/taoensso/nippy.clj index dfebccb..76f57b0 100644 --- a/src/taoensso/nippy.clj +++ b/src/taoensso/nippy.clj @@ -65,7 +65,9 @@ (def ^:const id-old-map (int 22)) ; as of 0.9.0, for more efficient thaw (def ^:const id-old-keyword (int 12)) ; as of 2.0.0-alpha5, for str consistecy -;;;; Shared low-level stream stuff +;;;; Freezing + +(defprotocol Freezable (freeze-to-stream* [this stream])) (defn- write-id [^DataOutputStream stream ^Integer id] (.writeByte stream id)) @@ -83,24 +85,6 @@ [^DataOutputStream stream ^String x] (write-bytes stream (.getBytes x "UTF-8"))) -(defn- read-bytes - ^bytes [^DataInputStream stream] - (let [size (.readInt stream) - ba (byte-array size)] - (.read stream ba 0 size) ba)) - -(defn- read-biginteger - ^BigInteger [^DataInputStream stream] - (BigInteger. (read-bytes stream))) - -(defn- read-utf8 - [^DataInputStream stream] - (String. (read-bytes stream) "UTF-8")) - -;;;; Freezing - -(defprotocol Freezable (freeze-to-stream* [this stream])) - (defn- freeze-to-stream "Like `freeze-to-stream*` but with metadata support." [x ^DataOutputStream s] @@ -206,6 +190,20 @@ (declare thaw-from-stream) +(defn- read-bytes + ^bytes [^DataInputStream stream] + (let [size (.readInt stream) + ba (byte-array size)] + (.read stream ba 0 size) ba)) + +(defn- read-biginteger + ^BigInteger [^DataInputStream stream] + (BigInteger. (read-bytes stream))) + +(defn- read-utf8 + [^DataInputStream stream] + (String. (read-bytes stream) "UTF-8")) + (defmacro ^:private coll-thaw "Thaws simple collection types." [s coll] `(let [s# ~s]