-
Notifications
You must be signed in to change notification settings - Fork 0
/
StreamingBody.clj
53 lines (44 loc) · 1.37 KB
/
StreamingBody.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
(ns unixsocket-http.impl.StreamingBody
"Implementation of OkHttp's RequestBody that streams some data."
(:gen-class
:name unixsocket_http.impl.StreamingBody
:extends okhttp3.RequestBody
:init init
:state state
:constructors {[java.io.InputStream] []
[java.io.InputStream String] []})
(:import [okhttp3 MediaType]
[okio BufferedSink Okio]
[java.io InputStream]))
;; ## Constructor
(def ^:private media-type-octetstream
(MediaType/get "application/octet-stream"))
(defn -init
([^InputStream stream]
[[] {:stream stream
:media-type media-type-octetstream}])
([^InputStream stream ^String content-type]
[[] {:stream stream
:media-type (if content-type
(MediaType/get content-type)
media-type-octetstream)}]))
(defn- stream
^InputStream [^unixsocket_http.impl.StreamingBody this]
(:stream (.-state this)))
(defn- media-type
^InputStream [^unixsocket_http.impl.StreamingBody this]
(:media-type (.-state this)))
;; ## Methods
(defn -contentType
[this]
(media-type this))
(defn -contentLength
[this]
(let [length (.available (stream this))]
(if (pos? length)
length
-1)))
(defn -writeTo
[this ^BufferedSink sink]
(with-open [source (Okio/source (stream this))]
(.writeAll sink source)))