[#70] move small? check outside write-bytes macro body, replace if-not's w/ if's (@postspectacular)
This commit is contained in:
parent
1ba3c38ab2
commit
3f9fe327e0
1 changed files with 20 additions and 18 deletions
|
|
@ -162,13 +162,13 @@
|
||||||
(defmacro write-id [out id] `(.writeByte ~out ~id))
|
(defmacro write-id [out id] `(.writeByte ~out ~id))
|
||||||
(defmacro write-bytes [out ba & [small?]]
|
(defmacro write-bytes [out ba & [small?]]
|
||||||
(let [out (with-meta out {:tag 'java.io.DataOutput})
|
(let [out (with-meta out {:tag 'java.io.DataOutput})
|
||||||
ba (with-meta ba {:tag 'bytes})]
|
ba (with-meta ba {:tag 'bytes})
|
||||||
|
;; Optimization, must be known before id's written
|
||||||
|
;; `byte` to throw on range error
|
||||||
|
[wc wr] (if small? [byte 'writeByte] [int 'writeInt])]
|
||||||
`(let [out# ~out, ba# ~ba
|
`(let [out# ~out, ba# ~ba
|
||||||
size# (alength ba#)]
|
size# (alength ba#)]
|
||||||
(if ~small? ; Optimization, must be known before id's written
|
(. out# ~wr (~wc size#))
|
||||||
(.writeByte out# (byte size#)) ; `byte` to throw on range error
|
|
||||||
(.writeInt out# (int size#)) ; `int` ''
|
|
||||||
)
|
|
||||||
(.write out# ba# 0 size#))))
|
(.write out# ba# 0 size#))))
|
||||||
|
|
||||||
(defmacro write-biginteger [out x]
|
(defmacro write-biginteger [out x]
|
||||||
|
|
@ -405,7 +405,7 @@
|
||||||
encryptor aes128-encryptor}
|
encryptor aes128-encryptor}
|
||||||
:as opts}]]
|
:as opts}]]
|
||||||
(let [legacy-mode? (:legacy-mode opts) ; DEPRECATED Nippy v1-compatible freeze
|
(let [legacy-mode? (:legacy-mode opts) ; DEPRECATED Nippy v1-compatible freeze
|
||||||
compressor (if-not legacy-mode? compressor snappy-compressor)
|
compressor (if legacy-mode? snappy-compressor compressor)
|
||||||
encryptor (when password (if-not legacy-mode? encryptor nil))
|
encryptor (when password (if-not legacy-mode? encryptor nil))
|
||||||
skip-header? (or skip-header? legacy-mode?)
|
skip-header? (or skip-header? legacy-mode?)
|
||||||
baos (ByteArrayOutputStream.)
|
baos (ByteArrayOutputStream.)
|
||||||
|
|
@ -423,8 +423,8 @@
|
||||||
compressor ; Assume compressor
|
compressor ; Assume compressor
|
||||||
))
|
))
|
||||||
|
|
||||||
ba (if-not compressor ba (compress compressor ba))
|
ba (if compressor (compress compressor ba) ba)
|
||||||
ba (if-not encryptor ba (encrypt encryptor password ba))]
|
ba (if encryptor (encrypt encryptor password ba) ba)]
|
||||||
|
|
||||||
(if skip-header? ba
|
(if skip-header? ba
|
||||||
(wrap-header ba
|
(wrap-header ba
|
||||||
|
|
@ -653,18 +653,20 @@
|
||||||
e)))
|
e)))
|
||||||
thaw-data
|
thaw-data
|
||||||
(fn [data-ba compressor-id encryptor-id]
|
(fn [data-ba compressor-id encryptor-id]
|
||||||
(let [compressor (if-not (identical? compressor :auto) compressor
|
(let [compressor (if (identical? compressor :auto)
|
||||||
(get-auto-compressor compressor-id))
|
(get-auto-compressor compressor-id)
|
||||||
encryptor (if-not (identical? encryptor :auto) encryptor
|
compressor)
|
||||||
(get-auto-encryptor encryptor-id))]
|
encryptor (if (identical? encryptor :auto)
|
||||||
|
(get-auto-encryptor encryptor-id)
|
||||||
|
encryptor)]
|
||||||
|
|
||||||
(when (and encryptor (not password))
|
(when (and encryptor (not password))
|
||||||
(ex "Password required for decryption."))
|
(ex "Password required for decryption."))
|
||||||
|
|
||||||
(try
|
(try
|
||||||
(let [ba data-ba
|
(let [ba data-ba
|
||||||
ba (if-not encryptor ba (decrypt encryptor password ba))
|
ba (if encryptor (decrypt encryptor password ba) ba)
|
||||||
ba (if-not compressor ba (decompress compressor ba))
|
ba (if compressor (decompress compressor ba) ba)
|
||||||
dis (DataInputStream. (ByteArrayInputStream. ba))]
|
dis (DataInputStream. (ByteArrayInputStream. ba))]
|
||||||
(thaw-from-in! dis))
|
(thaw-from-in! dis))
|
||||||
|
|
||||||
|
|
@ -791,9 +793,9 @@
|
||||||
ba-len (alength ba)
|
ba-len (alength ba)
|
||||||
compress? (> ba-len 1024)]
|
compress? (> ba-len 1024)]
|
||||||
(.writeBoolean out compress?)
|
(.writeBoolean out compress?)
|
||||||
(if-not compress? (write-bytes out ba)
|
(if compress?
|
||||||
(let [ba* (compress lzma2-compressor ba)]
|
(write-bytes out (compress lzma2-compressor ba))
|
||||||
(write-bytes out ba*)))))
|
(write-bytes out ba))))
|
||||||
|
|
||||||
(extend-thaw 128 [in]
|
(extend-thaw 128 [in]
|
||||||
(let [compressed? (.readBoolean in)
|
(let [compressed? (.readBoolean in)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue