[nop] Use UTF8 charset const

This commit is contained in:
Peter Taoussanis 2023-01-25 15:24:50 +01:00
parent 621f1189c7
commit 0a15278206

View file

@ -11,6 +11,7 @@
[encryption :as encryption]]) [encryption :as encryption]])
(:import (:import
[java.nio.charset StandardCharsets]
[java.io ByteArrayInputStream ByteArrayOutputStream DataInputStream [java.io ByteArrayInputStream ByteArrayOutputStream DataInputStream
DataOutputStream Serializable ObjectOutputStream ObjectInputStream DataOutputStream Serializable ObjectOutputStream ObjectInputStream
DataOutput DataInput] DataOutput DataInput]
@ -49,8 +50,7 @@
;; [2] See `IFreezable1` protocol for type-specific payload formats, ;; [2] See `IFreezable1` protocol for type-specific payload formats,
;; `thaw-from-in!` for reference type-specific thaw implementations ;; `thaw-from-in!` for reference type-specific thaw implementations
;; ;;
(def ^:private ^:const charset "UTF-8") (def ^:private head-sig "First 3 bytes of Nippy header" (.getBytes "NPY" StandardCharsets/UTF_8))
(def ^:private head-sig "First 3 bytes of Nippy header" (.getBytes "NPY" charset))
(def ^:private ^:const head-version "Current Nippy header format version" 1) (def ^:private ^:const head-version "Current Nippy header format version" 1)
(def ^:private ^:const head-meta (def ^:private ^:const head-meta
"Final byte of 4-byte Nippy header stores version-dependent metadata" "Final byte of 4-byte Nippy header stores version-dependent metadata"
@ -655,13 +655,13 @@
(defn- write-biginteger [out ^BigInteger n] (write-bytes-lg out (.toByteArray n))) (defn- write-biginteger [out ^BigInteger n] (write-bytes-lg out (.toByteArray n)))
(defn- write-str-sm [^DataOutput out ^String s] (write-bytes-sm out (.getBytes s charset))) (defn- write-str-sm [^DataOutput out ^String s] (write-bytes-sm out (.getBytes s StandardCharsets/UTF_8)))
(defn- write-str-md [^DataOutput out ^String s] (write-bytes-md out (.getBytes s charset))) (defn- write-str-md [^DataOutput out ^String s] (write-bytes-md out (.getBytes s StandardCharsets/UTF_8)))
(defn- write-str-lg [^DataOutput out ^String s] (write-bytes-lg out (.getBytes s charset))) (defn- write-str-lg [^DataOutput out ^String s] (write-bytes-lg out (.getBytes s StandardCharsets/UTF_8)))
(defn- write-str [^DataOutput out ^String s] (defn- write-str [^DataOutput out ^String s]
(if (identical? s "") (if (identical? s "")
(write-id out id-str-0) (write-id out id-str-0)
(let [ba (.getBytes s charset) (let [ba (.getBytes s StandardCharsets/UTF_8)
len (alength ba)] len (alength ba)]
(enc/cond (enc/cond
(sm-count? len) (do (write-id out id-str-sm) (write-sm-count out len)) (sm-count? len) (do (write-id out id-str-sm) (write-sm-count out len))
@ -672,7 +672,7 @@
(defn- write-kw [^DataOutput out kw] (defn- write-kw [^DataOutput out kw]
(let [s (if-let [ns (namespace kw)] (str ns "/" (name kw)) (name kw)) (let [s (if-let [ns (namespace kw)] (str ns "/" (name kw)) (name kw))
ba (.getBytes s charset) ba (.getBytes s StandardCharsets/UTF_8)
len (alength ba)] len (alength ba)]
(enc/cond (enc/cond
(sm-count? len) (do (write-id out id-kw-sm) (write-sm-count out len)) (sm-count? len) (do (write-id out id-kw-sm) (write-sm-count out len))
@ -684,7 +684,7 @@
(defn- write-sym [^DataOutput out s] (defn- write-sym [^DataOutput out s]
(let [s (if-let [ns (namespace s)] (str ns "/" (name s)) (name s)) (let [s (if-let [ns (namespace s)] (str ns "/" (name s)) (name s))
ba (.getBytes s charset) ba (.getBytes s StandardCharsets/UTF_8)
len (alength ba)] len (alength ba)]
(enc/cond (enc/cond
(sm-count? len) (do (write-id out id-sym-sm) (write-sm-count out len)) (sm-count? len) (do (write-id out id-sym-sm) (write-sm-count out len))
@ -858,7 +858,7 @@
(defn- write-serializable [^DataOutput out x ^String class-name] (defn- write-serializable [^DataOutput out x ^String class-name]
(when-debug (println (str "write-serializable: " (type x)))) (when-debug (println (str "write-serializable: " (type x))))
(let [class-name-ba (.getBytes class-name charset) (let [class-name-ba (.getBytes class-name StandardCharsets/UTF_8)
len (alength class-name-ba)] len (alength class-name-ba)]
(enc/cond (enc/cond
@ -882,7 +882,7 @@
(defn- write-readable [^DataOutput out x] (defn- write-readable [^DataOutput out x]
(when-debug (println (str "write-readable: " (type x)))) (when-debug (println (str "write-readable: " (type x))))
(let [edn (enc/pr-edn x) (let [edn (enc/pr-edn x)
edn-ba (.getBytes ^String edn charset) edn-ba (.getBytes ^String edn StandardCharsets/UTF_8)
len (alength edn-ba)] len (alength edn-ba)]
(enc/cond (enc/cond
(sm-count? len) (do (write-id out id-reader-sm) (write-bytes-sm out edn-ba)) (sm-count? len) (do (write-id out id-reader-sm) (write-bytes-sm out edn-ba))
@ -1098,7 +1098,7 @@
(freezer ISeq (write-coll out id-seq-0 id-seq-sm id-seq-md id-seq-lg x)) (freezer ISeq (write-coll out id-seq-0 id-seq-sm id-seq-md id-seq-lg x))
(freezer IRecord (freezer IRecord
(let [class-name (.getName (class x)) ; Reflect (let [class-name (.getName (class x)) ; Reflect
class-name-ba (.getBytes class-name charset) class-name-ba (.getBytes class-name StandardCharsets/UTF_8)
len (alength class-name-ba)] len (alength class-name-ba)]
(enc/cond (enc/cond
(sm-count? len) (do (write-id out id-record-sm) (write-bytes-sm out class-name-ba)) (sm-count? len) (do (write-id out id-record-sm) (write-bytes-sm out class-name-ba))
@ -1306,17 +1306,17 @@
id-bytes-md (read-bytes in (read-md-count in)) id-bytes-md (read-bytes in (read-md-count in))
id-bytes-lg (read-bytes in (read-lg-count in))))) id-bytes-lg (read-bytes in (read-lg-count in)))))
(defn- read-str-sm [^DataInput in] (String. ^bytes (read-bytes in (read-sm-count in)) charset)) (defn- read-str-sm [^DataInput in] (String. ^bytes (read-bytes in (read-sm-count in)) StandardCharsets/UTF_8))
(defn- read-str-md [^DataInput in] (String. ^bytes (read-bytes in (read-md-count in)) charset)) (defn- read-str-md [^DataInput in] (String. ^bytes (read-bytes in (read-md-count in)) StandardCharsets/UTF_8))
(defn- read-str-lg [^DataInput in] (String. ^bytes (read-bytes in (read-lg-count in)) charset)) (defn- read-str-lg [^DataInput in] (String. ^bytes (read-bytes in (read-lg-count in)) StandardCharsets/UTF_8))
(defn- read-str (defn- read-str
([^DataInput in len] (String. ^bytes (read-bytes in len) charset)) ([^DataInput in len] (String. ^bytes (read-bytes in len) StandardCharsets/UTF_8))
([^DataInput in ] ([^DataInput in ]
(enc/case-eval (.readByte in) (enc/case-eval (.readByte in)
id-str-0 "" id-str-0 ""
id-str-sm (String. ^bytes (read-bytes in (read-sm-count in)) charset) id-str-sm (String. ^bytes (read-bytes in (read-sm-count in)) StandardCharsets/UTF_8)
id-str-md (String. ^bytes (read-bytes in (read-md-count in)) charset) id-str-md (String. ^bytes (read-bytes in (read-md-count in)) StandardCharsets/UTF_8)
id-str-lg (String. ^bytes (read-bytes in (read-lg-count in)) charset)))) id-str-lg (String. ^bytes (read-bytes in (read-lg-count in)) StandardCharsets/UTF_8))))
(defn- read-biginteger [^DataInput in] (BigInteger. ^bytes (read-bytes in (.readInt in)))) (defn- read-biginteger [^DataInput in] (BigInteger. ^bytes (read-bytes in (.readInt in))))
@ -2015,8 +2015,8 @@
(let [[first2bytes nextbytes] (enc/ba-split ba 2) (let [[first2bytes nextbytes] (enc/ba-split ba 2)
?known-wrapper ?known-wrapper
(enc/cond (enc/cond
(enc/ba= first2bytes (.getBytes "\u0000<" charset)) :carmine/bin (enc/ba= first2bytes (.getBytes "\u0000<" StandardCharsets/UTF_8)) :carmine/bin
(enc/ba= first2bytes (.getBytes "\u0000>" charset)) :carmine/clj) (enc/ba= first2bytes (.getBytes "\u0000>" StandardCharsets/UTF_8)) :carmine/clj)
unwrapped-ba (if ?known-wrapper nextbytes ba) unwrapped-ba (if ?known-wrapper nextbytes ba)
[data-ba ?nippy-header] (or (try-parse-header unwrapped-ba) [data-ba ?nippy-header] (or (try-parse-header unwrapped-ba)