-
Notifications
You must be signed in to change notification settings - Fork 2
/
Handlers.clj
164 lines (142 loc) · 4.41 KB
/
Handlers.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
(ns clj-curl.Handlers
(:import [com.sun.jna Callback Pointer]
[java.io ByteArrayInputStream ByteArrayOutputStream File FileOutputStream FileInputStream]))
(gen-class
;This class opens a buffer and write all the received data to it.
;Made to be used with WRITEFUNCTION.
:name clj_curl.Handlers.MemHandler
:implements [com.sun.jna.Callback clojure.lang.IDeref AutoCloseable]
:init init
:constructors {[] []}
:state state
:prefix "memhandler-"
:methods [[^{Override {}} callback [com.sun.jna.Pointer int int com.sun.jna.Pointer] int]
[getString [] String]
[getBytes [] bytes]
[getSize [] int]
[deref [] String]
[close [] Void]])
(defn memhandler-init
[]
[[] (ByteArrayOutputStream.)])
(defn memhandler-callback
[this contents size nmemb userp]
(try
(let [^int s (* size nmemb)
^bytes data (.getByteArray contents 0 s)]
(.write (.state this) data)
s)
(catch Exception e
(.printStackTrace e))))
(defn memhandler-getBytes
[this]
(.toByteArray (.state this)))
(defn memhandler-getString
[this]
(.toString (.state this)))
(defn memhandler-getSize
[this]
(count (.getBytes this)))
(defn memhandler-deref
[this]
(.getString this))
(defn memhandler-close
[this]
(-> this .state .close))
(gen-class
;This class opens a file and write all the received data to it.
;It was made to be used with WRITEFUNCTION.
;FileHandler will overwrite all of the data to the file named filename if it already exists.
:name clj_curl.Handlers.FileHandler
:implements [com.sun.jna.Callback AutoCloseable]
:init init
:constructors {[String] []}
:state state
:prefix "filehandler-"
:methods [[^{Override {}} callback [com.sun.jna.Pointer int int com.sun.jna.Pointer] int]
[getFilename [] String]
[close [] Void]])
(defn filehandler-init
[filename]
[[] {:filename filename :stream (-> filename File. FileOutputStream.)}])
(defn filehandler-callback
[this contents size nmemb userp]
(try
(let [^int s (* size nmemb)
^bytes data (.getByteArray contents 0 s)]
(do
(.write (-> this .state :stream) data)
s))
(catch Exception e
(.printStackTrace e))))
(defn filehandler-getFilename
[this]
(-> this .state :filename))
(defn filehandler-close
[this]
(-> this .state :stream .close))
(gen-class
;This class opens a populated buffer and waits for something consume its data.
;Made to be used with READFUNCTION
:name clj_curl.Handlers.MemReader
:implements [com.sun.jna.Callback AutoCloseable]
:init init
:constructors {[bytes] []
[String] []}
:state state
:prefix "memreader-"
:methods [[^{Override {}} callback [com.sun.jna.Pointer int int com.sun.jna.Pointer] int]
[close [] Void]])
(defn memreader-init
[array]
[[] (ByteArrayInputStream. (byte-array (map byte array)))])
(defn memreader-callback
[this contents size nmemb userp]
(try
(let [^int s (* size nmemb)
^bytes buffer (byte-array s nil)
^int n-read-bytes (.read (.state this) buffer 0 s)]
(if (= -1 n-read-bytes)
0
(do
(.write contents 0 buffer 0 n-read-bytes)
n-read-bytes)))
(catch Exception e
(.printStackTrace e))))
(defn memreader-close
[this]
(-> this .state .close))
(gen-class
;This class opens a file, read it and waits for somethind consume its data.
;Made to be used with READFUNCTION
:name clj_curl.Handlers.FileReader
:implements [com.sun.jna.Callback AutoCloseable]
:init init
:constructors {[String] []}
:state state
:prefix "filereader-"
:methods [[^{Override {}} callback [com.sun.jna.Pointer int int com.sun.jna.Pointer] int]
[getFilename [] String]
[close [] Void]])
(defn filereader-init
[filename]
[[] {:filename filename :stream (-> filename File. FileInputStream.)}])
(defn filereader-callback
[this contents size nmemb userp]
(try
(let [^int s (* size nmemb)
^bytes buffer (byte-array s nil)
^int n-read-bytes (.read (-> this .state :stream) buffer 0 s)]
(if (= -1 n-read-bytes)
0
(do
(.write contents 0 buffer 0 n-read-bytes)
n-read-bytes)))
(catch Exception e
(.printStackTrace e))))
(defn filereader-getFilename
[this]
(-> this .state :filename))
(defn filereader-close
[this]
(-> this .state :stream .close))