forked from hiredman/clj-http-lite
-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.clj
82 lines (71 loc) · 2.52 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
(ns clj-http.lite.util
"Helper functions for the HTTP client."
(:require [clojure.java.io :as io])
(:import (java.io ByteArrayInputStream ByteArrayOutputStream InputStream)
(java.net URLEncoder URLDecoder)
(java.util.zip InflaterInputStream DeflaterInputStream
GZIPInputStream GZIPOutputStream)))
(defn utf8-bytes
"Returns the UTF-8 bytes corresponding to the given string."
[#^String s]
(.getBytes s "UTF-8"))
(defn utf8-string
"Returns the String corresponding to the UTF-8 decoding of the given bytes."
[#^"[B" b]
(String. b "UTF-8"))
(defn url-decode
"Returns the form-url-decoded version of the given string, using either a
specified encoding or UTF-8 by default."
[^String encoded & [encoding]]
(let [^String encoding (or encoding "UTF-8")]
(URLDecoder/decode encoded encoding)))
(defn url-encode
"Returns an UTF-8 URL encoded version of the given string."
[^String unencoded]
(URLEncoder/encode unencoded "UTF-8"))
(defmacro base64-encode
"Encode an array of bytes into a base64 encoded string."
[unencoded]
(if (try (import 'javax.xml.bind.DatatypeConverter)
(catch ClassNotFoundException _))
`(javax.xml.bind.DatatypeConverter/printBase64Binary ~unencoded)
(do
(import 'java.util.Base64)
`(.encodeToString (java.util.Base64/getEncoder) ~unencoded))))
(defn to-byte-array
"Returns a byte array for the InputStream provided."
[is]
(let [chunk-size 8192
baos (ByteArrayOutputStream.)
buffer (byte-array chunk-size)]
(loop [len (.read ^InputStream is buffer 0 chunk-size)]
(when (not= -1 len)
(.write baos buffer 0 len)
(recur (.read ^InputStream is buffer 0 chunk-size))))
(.toByteArray baos)))
(defn gunzip
"Returns a gunzip'd version of the given byte array."
[b]
(when b
(if (instance? InputStream b)
(GZIPInputStream. b)
(to-byte-array (GZIPInputStream. (ByteArrayInputStream. b))))))
(defn gzip
"Returns a gzip'd version of the given byte array."
[b]
(when b
(let [baos (ByteArrayOutputStream.)
gos (GZIPOutputStream. baos)]
(io/copy (ByteArrayInputStream. b) gos)
(.close gos)
(.toByteArray baos))))
(defn inflate
"Returns a zlib inflate'd version of the given byte array."
[b]
(when b
(to-byte-array (InflaterInputStream. (ByteArrayInputStream. b)))))
(defn deflate
"Returns a deflate'd version of the given byte array."
[b]
(when b
(to-byte-array (DeflaterInputStream. (ByteArrayInputStream. b)))))