Reorganize low-level stream fns (prepping for perf work)

This commit is contained in:
Peter Taoussanis 2013-06-16 12:51:30 +07:00
parent c5d039b183
commit 26ad8dd252

View file

@ -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]