forked from dakrone/clj-http
/
util.clj
48 lines (40 loc) · 1.43 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
(ns clj-http.util
"Helper functions for the HTTP client."
(:import (java.net URLEncoder))
(:import (org.apache.commons.codec.binary Base64))
(:import (java.io ByteArrayInputStream ByteArrayOutputStream))
(:import (java.util.zip InflaterInputStream DeflaterInputStream
GZIPInputStream GZIPOutputStream))
(:import (org.apache.commons.io IOUtils)))
(defn utf8-bytes [#^String s]
(.getBytes s "UTF-8"))
(defn utf8-string [b]
(String. b "UTF-8"))
(defn url-encode
"Returns an UTF-8 URL encoded version of the given string."
[unencoded]
(URLEncoder/encode unencoded "UTF-8"))
(defn base64-encode
"Encode an array of bytes into a base64 encoded string."
[unencoded]
(utf8-string (Base64/encodeBase64 unencoded)))
(defn gunzip
"Returns a gunzip'd version of the given byte array."
[b]
(IOUtils/toByteArray (GZIPInputStream. (ByteArrayInputStream. b))))
(defn gzip
"Returns a gzip'd version of the given byte array."
[b]
(let [baos (ByteArrayOutputStream.)
gos (GZIPOutputStream. baos)]
(IOUtils/copy (ByteArrayInputStream. b) gos)
(.close gos)
(.toByteArray baos)))
(defn inflate
"Returns a zlib inflate'd version of the given byte array."
[b]
(IOUtils/toByteArray (InflaterInputStream. (ByteArrayInputStream. b))))
(defn deflate
"Returns a deflate'd version of the given byte array."
[b]
(IOUtils/toByteArray (DeflaterInputStream. (ByteArrayInputStream. b))))