|
27 | 27 | (defmacro cond-let [bindings & clauses] |
28 | 28 | `(let ~bindings (cond ~@clauses))) |
29 | 29 |
|
| 30 | +(defn- pack-raw |
| 31 | + [^bytes bytes ^java.io.DataOutput s] |
| 32 | + (cond-let [len (count bytes)] |
| 33 | + (<= len 0x1f) |
| 34 | + (do (.writeByte s (bit-or 2r10100000 len)) (.write s bytes)) |
| 35 | + |
| 36 | + (<= len 0xffff) |
| 37 | + (do (.writeByte s 0xda) (.writeShort s len) (.write s bytes)) |
| 38 | + |
| 39 | + (<= len 0xffffffff) |
| 40 | + (do (.writeByte s 0xdb) (.writeInt s len) (.write s bytes)))) |
| 41 | + |
30 | 42 | (defn- pack-str |
31 | | - [^bytes bytes ^java.io.DataOutput s {:keys [raw]}] |
| 43 | + [^bytes bytes ^java.io.DataOutput s] |
32 | 44 | (cond-let [len (count bytes)] |
33 | 45 | (<= len 0x1f) |
34 | 46 | (do (.writeByte s (bit-or 2r10100000 len)) (.write s bytes)) |
35 | 47 |
|
36 | 48 | (<= len 0xff) |
37 | | - (if raw |
38 | | - (do (.writeByte s 0xda) (.writeShort s len) (.write s bytes)) |
39 | | - (do (.writeByte s 0xd9) (.writeByte s len) (.write s bytes))) |
| 49 | + (do (.writeByte s 0xd9) (.writeByte s len) (.write s bytes)) |
40 | 50 |
|
41 | 51 | (<= len 0xffff) |
42 | 52 | (do (.writeByte s 0xda) (.writeShort s len) (.write s bytes)) |
|
45 | 55 | (do (.writeByte s 0xdb) (.writeInt s len) (.write s bytes)))) |
46 | 56 |
|
47 | 57 | (defn- pack-bytes |
48 | | - [^bytes bytes ^java.io.DataOutput s {:keys [raw] :as opts}] |
49 | | - (if raw |
50 | | - (pack-str bytes s opts) |
51 | | - (cond-let [len (count bytes)] |
52 | | - (<= len 0xff) |
53 | | - (do (.writeByte s 0xc4) (.writeByte s len) (.write s bytes)) |
| 58 | + [^bytes bytes ^java.io.DataOutput s] |
| 59 | + (cond-let [len (count bytes)] |
| 60 | + (<= len 0xff) |
| 61 | + (do (.writeByte s 0xc4) (.writeByte s len) (.write s bytes)) |
54 | 62 |
|
55 | | - (<= len 0xffff) |
56 | | - (do (.writeByte s 0xc5) (.writeShort s len) (.write s bytes)) |
| 63 | + (<= len 0xffff) |
| 64 | + (do (.writeByte s 0xc5) (.writeShort s len) (.write s bytes)) |
57 | 65 |
|
58 | | - (<= len 0xffffffff) |
59 | | - (do (.writeByte s 0xc6) (.writeInt s len) (.write s bytes))))) |
| 66 | + (<= len 0xffffffff) |
| 67 | + (do (.writeByte s 0xc6) (.writeInt s len) (.write s bytes)))) |
60 | 68 |
|
61 | 69 | (defn- pack-int |
62 | 70 | "Pack integer using the most compact representation" |
|
133 | 141 |
|
134 | 142 | java.lang.String |
135 | 143 | (pack-stream |
136 | | - [str ^java.io.DataOutput s opts] |
137 | | - (pack-str (.getBytes ^String str msgpack-charset) s opts)) |
| 144 | + [str ^java.io.DataOutput s {:keys [raw]}] |
| 145 | + (let [bytes (.getBytes ^String str msgpack-charset)] |
| 146 | + (if raw |
| 147 | + (pack-raw bytes s) |
| 148 | + (pack-str bytes s)))) |
138 | 149 |
|
139 | 150 | Ext |
140 | 151 | (pack-stream |
|
187 | 198 | (extend (class (java.lang.reflect.Array/newInstance Byte 0)) |
188 | 199 | Packable |
189 | 200 | {:pack-stream |
190 | | - (fn ([bytes ^java.io.DataOutput s opts] |
191 | | - (pack-bytes bytes s opts)))}) |
| 201 | + (fn [bytes ^java.io.DataOutput s {:keys [raw]}] |
| 202 | + (if raw |
| 203 | + (pack-raw bytes s) |
| 204 | + (pack-bytes bytes s)))}) |
192 | 205 |
|
193 | 206 | (extend (Class/forName "[B") |
194 | 207 | Packable |
195 | 208 | {:pack-stream |
196 | | - (fn ([bytes ^java.io.DataOutput s opts] |
197 | | - (pack-bytes bytes s opts)))}) |
| 209 | + (fn [bytes ^java.io.DataOutput s {:keys [raw]}] |
| 210 | + (if raw |
| 211 | + (pack-raw bytes s) |
| 212 | + (pack-bytes bytes s)))}) |
198 | 213 |
|
199 | 214 | (defn pack |
200 | 215 | ([obj] (pack obj nil)) |
|
232 | 247 | bytes))) |
233 | 248 |
|
234 | 249 | (defn- read-str |
235 | | - [n ^java.io.DataInput data-input] |
236 | | - (String. ^bytes (read-bytes n data-input) msgpack-charset)) |
| 250 | + [n ^java.io.DataInput data-input {:keys [raw]}] |
| 251 | + (if raw |
| 252 | + (read-bytes n data-input) |
| 253 | + (String. ^bytes (read-bytes n data-input) msgpack-charset))) |
237 | 254 |
|
238 | 255 | (defmulti refine-ext |
239 | 256 | "Refine Extended type to an application-specific type." |
|
251 | 268 | (defn- unpack-map [n ^java.io.DataInput data-input] |
252 | 269 | (apply hash-map (unpack-n (* 2 n) data-input))) |
253 | 270 |
|
254 | | -(defn unpack-stream [^java.io.DataInput data-input] |
255 | | - (cond-let [byte (.readUnsignedByte data-input)] |
256 | | - ; nil format family |
257 | | - (= byte 0xc0) nil |
| 271 | +(defn unpack-stream |
| 272 | + ([^java.io.DataInput data-input] (unpack-stream data-input nil)) |
| 273 | + ([^java.io.DataInput data-input opts] |
| 274 | + (cond-let [byte (.readUnsignedByte data-input)] |
| 275 | + ; nil format family |
| 276 | + (= byte 0xc0) nil |
258 | 277 |
|
259 | | - ; bool format family |
260 | | - (= byte 0xc2) false |
261 | | - (= byte 0xc3) true |
| 278 | + ; bool format family |
| 279 | + (= byte 0xc2) false |
| 280 | + (= byte 0xc3) true |
262 | 281 |
|
263 | | - ; int format family |
264 | | - (= (bit-and 2r11100000 byte) 2r11100000) |
265 | | - (unchecked-byte byte) |
| 282 | + ; int format family |
| 283 | + (= (bit-and 2r11100000 byte) 2r11100000) |
| 284 | + (unchecked-byte byte) |
266 | 285 |
|
267 | | - (= (bit-and 2r10000000 byte) 0) |
268 | | - (unchecked-byte byte) |
| 286 | + (= (bit-and 2r10000000 byte) 0) |
| 287 | + (unchecked-byte byte) |
269 | 288 |
|
270 | | - (= byte 0xcc) (read-uint8 data-input) |
271 | | - (= byte 0xcd) (read-uint16 data-input) |
272 | | - (= byte 0xce) (read-uint32 data-input) |
273 | | - (= byte 0xcf) (read-uint64 data-input) |
274 | | - (= byte 0xd0) (.readByte data-input) |
275 | | - (= byte 0xd1) (.readShort data-input) |
276 | | - (= byte 0xd2) (.readInt data-input) |
277 | | - (= byte 0xd3) (.readLong data-input) |
| 289 | + (= byte 0xcc) (read-uint8 data-input) |
| 290 | + (= byte 0xcd) (read-uint16 data-input) |
| 291 | + (= byte 0xce) (read-uint32 data-input) |
| 292 | + (= byte 0xcf) (read-uint64 data-input) |
| 293 | + (= byte 0xd0) (.readByte data-input) |
| 294 | + (= byte 0xd1) (.readShort data-input) |
| 295 | + (= byte 0xd2) (.readInt data-input) |
| 296 | + (= byte 0xd3) (.readLong data-input) |
278 | 297 |
|
279 | | - ; float format family |
280 | | - (= byte 0xca) (.readFloat data-input) |
281 | | - (= byte 0xcb) (.readDouble data-input) |
| 298 | + ; float format family |
| 299 | + (= byte 0xca) (.readFloat data-input) |
| 300 | + (= byte 0xcb) (.readDouble data-input) |
282 | 301 |
|
283 | | - ; str format family |
284 | | - (= (bit-and 2r11100000 byte) 2r10100000) |
285 | | - (let [n (bit-and 2r11111 byte)] |
286 | | - (read-str n data-input)) |
| 302 | + ; str format family |
| 303 | + (= (bit-and 2r11100000 byte) 2r10100000) |
| 304 | + (let [n (bit-and 2r11111 byte)] |
| 305 | + (read-str n data-input opts)) |
287 | 306 |
|
288 | | - (= byte 0xd9) |
289 | | - (read-str (read-uint8 data-input) data-input) |
| 307 | + (= byte 0xd9) |
| 308 | + (read-str (read-uint8 data-input) data-input opts) |
290 | 309 |
|
291 | | - (= byte 0xda) |
292 | | - (read-str (read-uint16 data-input) data-input) |
| 310 | + (= byte 0xda) |
| 311 | + (read-str (read-uint16 data-input) data-input opts) |
293 | 312 |
|
294 | | - (= byte 0xdb) |
295 | | - (read-str (read-uint32 data-input) data-input) |
| 313 | + (= byte 0xdb) |
| 314 | + (read-str (read-uint32 data-input) data-input opts) |
296 | 315 |
|
297 | | - ; bin format family |
298 | | - (= byte 0xc4) |
299 | | - (read-bytes (read-uint8 data-input) data-input) |
| 316 | + ; bin format family |
| 317 | + (= byte 0xc4) |
| 318 | + (read-bytes (read-uint8 data-input) data-input) |
300 | 319 |
|
301 | | - (= byte 0xc5) |
302 | | - (read-bytes (read-uint16 data-input) data-input) |
| 320 | + (= byte 0xc5) |
| 321 | + (read-bytes (read-uint16 data-input) data-input) |
303 | 322 |
|
304 | | - (= byte 0xc6) |
305 | | - (read-bytes (read-uint32 data-input) data-input) |
| 323 | + (= byte 0xc6) |
| 324 | + (read-bytes (read-uint32 data-input) data-input) |
306 | 325 |
|
307 | | - ; ext format family |
308 | | - (= byte 0xd4) (unpack-ext 1 data-input) |
309 | | - (= byte 0xd5) (unpack-ext 2 data-input) |
310 | | - (= byte 0xd6) (unpack-ext 4 data-input) |
311 | | - (= byte 0xd7) (unpack-ext 8 data-input) |
312 | | - (= byte 0xd8) (unpack-ext 16 data-input) |
| 326 | + ; ext format family |
| 327 | + (= byte 0xd4) (unpack-ext 1 data-input) |
| 328 | + (= byte 0xd5) (unpack-ext 2 data-input) |
| 329 | + (= byte 0xd6) (unpack-ext 4 data-input) |
| 330 | + (= byte 0xd7) (unpack-ext 8 data-input) |
| 331 | + (= byte 0xd8) (unpack-ext 16 data-input) |
313 | 332 |
|
314 | | - (= byte 0xc7) |
315 | | - (unpack-ext (read-uint8 data-input) data-input) |
| 333 | + (= byte 0xc7) |
| 334 | + (unpack-ext (read-uint8 data-input) data-input) |
316 | 335 |
|
317 | | - (= byte 0xc8) |
318 | | - (unpack-ext (read-uint16 data-input) data-input) |
| 336 | + (= byte 0xc8) |
| 337 | + (unpack-ext (read-uint16 data-input) data-input) |
319 | 338 |
|
320 | | - (= byte 0xc9) |
321 | | - (unpack-ext (read-uint32 data-input) data-input) |
| 339 | + (= byte 0xc9) |
| 340 | + (unpack-ext (read-uint32 data-input) data-input) |
322 | 341 |
|
323 | | - ; array format family |
324 | | - (= (bit-and 2r11110000 byte) 2r10010000) |
325 | | - (unpack-n (bit-and 2r1111 byte) data-input) |
| 342 | + ; array format family |
| 343 | + (= (bit-and 2r11110000 byte) 2r10010000) |
| 344 | + (unpack-n (bit-and 2r1111 byte) data-input) |
326 | 345 |
|
327 | | - (= byte 0xdc) |
328 | | - (unpack-n (read-uint16 data-input) data-input) |
| 346 | + (= byte 0xdc) |
| 347 | + (unpack-n (read-uint16 data-input) data-input) |
329 | 348 |
|
330 | | - (= byte 0xdd) |
331 | | - (unpack-n (read-uint32 data-input) data-input) |
| 349 | + (= byte 0xdd) |
| 350 | + (unpack-n (read-uint32 data-input) data-input) |
332 | 351 |
|
333 | | - ; map format family |
334 | | - (= (bit-and 2r11110000 byte) 2r10000000) |
335 | | - (unpack-map (bit-and 2r1111 byte) data-input) |
| 352 | + ; map format family |
| 353 | + (= (bit-and 2r11110000 byte) 2r10000000) |
| 354 | + (unpack-map (bit-and 2r1111 byte) data-input) |
336 | 355 |
|
337 | | - (= byte 0xde) |
338 | | - (unpack-map (read-uint16 data-input) data-input) |
| 356 | + (= byte 0xde) |
| 357 | + (unpack-map (read-uint16 data-input) data-input) |
339 | 358 |
|
340 | | - (= byte 0xdf) |
341 | | - (unpack-map (read-uint32 data-input) data-input))) |
| 359 | + (= byte 0xdf) |
| 360 | + (unpack-map (read-uint32 data-input) data-input)))) |
342 | 361 |
|
343 | 362 | (defn- to-byte-array |
344 | 363 | [bytes] |
|
348 | 367 |
|
349 | 368 | (defn unpack |
350 | 369 | "Unpack bytes as MessagePack object." |
351 | | - [bytes] |
352 | | - (-> bytes |
353 | | - to-byte-array |
354 | | - ByteArrayInputStream. |
355 | | - DataInputStream. |
356 | | - unpack-stream)) |
| 370 | + ([bytes] (unpack bytes nil)) |
| 371 | + ([bytes opts] |
| 372 | + (let [data-input (-> bytes |
| 373 | + to-byte-array |
| 374 | + ByteArrayInputStream. |
| 375 | + DataInputStream.)] |
| 376 | + (unpack-stream data-input opts)))) |
0 commit comments