|
23 | 23 | ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
24 | 24 |
|
25 | 25 | (ns octet.util
|
26 |
| - (:require [clojure.string :as str :refer [join]] |
27 |
| - [octet.buffer :as bfr]) |
| 26 | + #?(:clj (:require [clojure.string :as str] |
| 27 | + [octet.buffer :as buffer])) |
28 | 28 |
|
29 | 29 | #?(:clj (:import java.util.Arrays)))
|
30 | 30 |
|
|
34 | 34 | (def ~sym ~sym2)
|
35 | 35 | (alter-meta! (var ~sym) merge (dissoc (meta (var ~sym2)) :name))))
|
36 | 36 |
|
37 |
| -(defn assoc-ordered [a-map key val & rest] |
| 37 | +(defn assoc-ordered |
38 | 38 | "assoc into an array-map, keeping insertion order. The normal clojure
|
39 | 39 | assoc function switches to hash maps on maps > size 10 and loses insertion order"
|
| 40 | + [a-map key val & rest] |
40 | 41 | (let [kvs (interleave (concat (keys a-map) (list key))
|
41 | 42 | (concat (vals a-map) (list val)))
|
42 | 43 | ret (apply array-map kvs)]
|
|
55 | 56 | fh (fn [_ b]
|
56 | 57 | (let [h (Integer/toHexString (bit-and b 0xFF))]
|
57 | 58 | (if (<= 0 b 15) (str "0" h) h)))]
|
58 |
| - (join (reductions fh (fh 0 f) r))))) |
| 59 | + (str/join (reductions fh (fh 0 f) r))))) |
59 | 60 |
|
60 | 61 | #?(:clj
|
61 | 62 | (defn byte->ascii
|
|
64 | 65 | (if (<= 32 (bit-and byte 0xFF) 127) (char byte) \.)))
|
65 | 66 |
|
66 | 67 | #?(:clj
|
67 |
| - (defn- bytes->ascii [^bytes bytes] |
| 68 | + (defn- bytes->ascii |
68 | 69 | "returns a 16-per-line printable ascii view of the bytes"
|
| 70 | + [^bytes bytes] |
69 | 71 | (->> bytes
|
70 | 72 | (map byte->ascii)
|
71 | 73 | (partition 16 16 " ")
|
72 |
| - (map join)))) |
| 74 | + (map str/join)))) |
73 | 75 |
|
74 | 76 | #?(:clj
|
75 |
| - (defn- format-hex-line [^String hex-line] |
| 77 | + (defn- format-hex-line |
76 | 78 | "formats a 'line' (32 hex chars) of hex output"
|
| 79 | + [^String hex-line] |
77 | 80 | (->> hex-line
|
78 | 81 | (partition-all 4)
|
79 |
| - (map join) |
| 82 | + (map str/join) |
80 | 83 | (split-at 4)
|
81 |
| - (map #(join " " %)) |
82 |
| - (join " ")))) |
| 84 | + (map #(str/join " " %)) |
| 85 | + (str/join " ")))) |
83 | 86 |
|
84 | 87 | #?(:clj
|
85 |
| - (defn- bytes->hexdump [^bytes bytes] |
| 88 | + (defn- bytes->hexdump |
86 | 89 | "formats a byte array to a sequence of formatted hex lines"
|
| 90 | + [^bytes bytes] |
87 | 91 | (->> bytes
|
88 |
| - bytes->hex |
89 |
| - (partition 32 32 (join (repeat 32 " "))) |
| 92 | + (bytes->hex) |
| 93 | + (partition 32 32 (str/join (repeat 32 " "))) |
90 | 94 | (map format-hex-line))))
|
91 | 95 |
|
92 | 96 | #?(:clj
|
93 |
| - (defn- copy-bytes [bytes offset size] |
| 97 | + (defn- copy-bytes |
94 | 98 | "utility function - copy bytes, return new byte array"
|
| 99 | + ^bytes |
| 100 | + [^bytes bytes ^long offset ^long size] |
95 | 101 | (let [size (if (nil? size) (alength bytes) size)]
|
96 | 102 | (if (and (= 0 offset) (= (alength bytes) size))
|
97 | 103 | bytes ; short circuit
|
98 |
| - (java.util.Arrays/copyOfRange bytes |
99 |
| - offset |
100 |
| - (+ offset size)))))) |
| 104 | + (Arrays/copyOfRange bytes |
| 105 | + offset |
| 106 | + ^long (+ offset size)))))) |
101 | 107 |
|
102 | 108 | #?(:clj
|
103 | 109 | (defn get-dump-bytes
|
104 | 110 | "utility function - return byte array from offset offset and with
|
105 | 111 | size size for nio ByteBuffer, netty ByteBuf, byte array, and String"
|
| 112 | + ^bytes |
106 | 113 | [x offset size]
|
107 | 114 | (cond
|
108 |
| - (and (satisfies? octet.buffer/IBufferBytes x) |
109 |
| - (satisfies? octet.buffer/IBufferLimit x)) |
110 |
| - (let [size (if (nil? size) (octet.buffer/get-capacity x) size)] |
111 |
| - (octet.buffer/read-bytes x offset size)) |
| 115 | + (and (satisfies? buffer/IBufferBytes x) |
| 116 | + (satisfies? buffer/IBufferLimit x)) |
| 117 | + (let [size (if (nil? size) (buffer/get-capacity x) size)] |
| 118 | + (buffer/read-bytes x offset size)) |
112 | 119 |
|
113 | 120 | (instance? (type (byte-array 0)) x)
|
114 | 121 | (copy-bytes x offset size)
|
115 | 122 |
|
116 | 123 | (instance? String x)
|
117 |
| - (copy-bytes (.getBytes x) offset size)))) |
| 124 | + (copy-bytes (.getBytes ^String x) offset size)))) |
118 | 125 |
|
119 | 126 |
|
120 | 127 | ; Example usage of hex-dump
|
|
156 | 163 | ascii (bytes->ascii bytes)
|
157 | 164 | offs (map #(format "%08x" %)
|
158 | 165 | (range offset (+ offset size 16) 16))
|
159 |
| - header (str " " (join (repeat 68 "-"))) |
| 166 | + header (str " " (str/join (repeat 68 "-"))) |
160 | 167 | border (if frame "|" "")
|
161 | 168 | lines (map #(str border %1 ": " %2 " " %3 border) offs dump ascii)
|
162 | 169 | lines (if frame (concat [header] lines [header]) lines)
|
163 |
| - result (join \newline lines)] |
| 170 | + result (str/join \newline lines)] |
164 | 171 | (if print (println result) result))))
|
0 commit comments