|
2 | 2 | (:refer-clojure :exclude [take])
|
3 | 3 | (:require
|
4 | 4 | [primitive-math :as p]
|
| 5 | + [byte-streams.utils :refer [doit]] |
5 | 6 | [manifold
|
6 | 7 | [utils :as u]
|
7 | 8 | [stream :as s]
|
|
32 | 33 | deferred
|
33 | 34 | ^boolean eager?])
|
34 | 35 |
|
| 36 | +(defn trigger [^Consumption c] |
| 37 | + (let [^ByteBuffer buf (.buf c)] |
| 38 | + (d/success! (.deferred c) (.position buf)))) |
| 39 | + |
35 | 40 | (defn put [^ByteBuffer src ^ByteBuffer dst]
|
36 | 41 | (let [l (.limit src)]
|
37 | 42 | (.limit src (p/+ (.position src) (p/min (.remaining src) (.remaining dst))))
|
|
121 | 126 | PushbackStream
|
122 | 127 |
|
123 | 128 | (put [_ buf]
|
124 |
| - ((either |
125 |
| - [do] |
126 |
| - [u/with-lock lock]) |
| 129 | + (let [[consumers d] |
| 130 | + ((either |
| 131 | + [do] |
| 132 | + [u/with-lock* lock]) |
| 133 | + |
| 134 | + (if closed? |
| 135 | + [nil |
| 136 | + (d/success-deferred false)] |
| 137 | + |
| 138 | + [(loop [acc []] |
| 139 | + (if-let [^Consumption c (.peek consumers)] |
| 140 | + (let [^ByteBuffer out (.buf c)] |
| 141 | + (put buf out) |
| 142 | + (when (or (.eager? c) (not (.hasRemaining out))) |
| 143 | + (.remove consumers) |
| 144 | + (recur (conj acc c)))) |
| 145 | + acc)) |
127 | 146 |
|
128 |
| - (if closed? |
129 |
| - (d/success-deferred false) |
| 147 | + (do |
| 148 | + (when (.hasRemaining buf) |
| 149 | + (.add buffer buf) |
| 150 | + (set! buffer-size (unchecked-int (p/+ buffer-size (.remaining buf))))) |
130 | 151 |
|
131 |
| - (do |
132 |
| - (loop [] |
133 |
| - (when-let [^Consumption c (.peek consumers)] |
134 |
| - (let [^ByteBuffer out (.buf c)] |
135 |
| - (put buf out) |
136 |
| - (when (or (.eager? c) (not (.hasRemaining out))) |
137 |
| - (.remove consumers) |
138 |
| - (d/success! (.deferred c) (.position out)) |
139 |
| - (recur))))) |
| 152 | + (cond |
140 | 153 |
|
141 |
| - (when (.hasRemaining buf) |
142 |
| - (.add buffer buf) |
143 |
| - (set! buffer-size (unchecked-int (p/+ buffer-size (.remaining buf))))) |
| 154 | + deferred |
| 155 | + deferred |
144 | 156 |
|
145 |
| - (cond |
| 157 | + (p/<= buffer-size buffer-capacity) |
| 158 | + (d/success-deferred true) |
146 | 159 |
|
147 |
| - deferred |
148 |
| - deferred |
| 160 | + :else |
| 161 | + (set! deferred (d/deferred))))]))] |
149 | 162 |
|
150 |
| - (p/<= buffer-size buffer-capacity) |
151 |
| - (d/success-deferred true) |
| 163 | + (when consumers |
| 164 | + (doit [c consumers] |
| 165 | + (trigger c))) |
152 | 166 |
|
153 |
| - :else |
154 |
| - (set! deferred (d/deferred))))))) |
| 167 | + d)) |
155 | 168 |
|
156 | 169 | (put [this ary offset length]
|
157 | 170 | (.put this
|
|
160 | 173 | (.limit (+ offset length)))))
|
161 | 174 |
|
162 | 175 | (pushback [_ buf]
|
163 |
| - ((either |
164 |
| - [do] |
165 |
| - [u/with-lock lock]) |
166 |
| - (loop [] |
167 |
| - (when-let [^Consumption c (.peek consumers)] |
168 |
| - (let [^ByteBuffer out (.buf c)] |
169 |
| - (put buf out) |
170 |
| - (when (or (.eager? c) (not (.hasRemaining out))) |
171 |
| - (.remove consumers) |
172 |
| - (d/success! (.deferred c) (.position out)) |
173 |
| - (recur))))) |
174 |
| - |
175 |
| - (when (.hasRemaining buf) |
176 |
| - (.addLast buffer buf) |
177 |
| - (set! buffer-size (unchecked-int (p/+ buffer-size (.remaining buf))))))) |
| 176 | + (let [consumers |
| 177 | + ((either |
| 178 | + [do] |
| 179 | + [u/with-lock* lock]) |
| 180 | + (let [consumers |
| 181 | + (loop [acc []] |
| 182 | + (if-let [^Consumption c (.peek consumers)] |
| 183 | + (let [^ByteBuffer out (.buf c)] |
| 184 | + (put buf out) |
| 185 | + (when (or (.eager? c) (not (.hasRemaining out))) |
| 186 | + (.remove consumers) |
| 187 | + (recur (conj acc c)))) |
| 188 | + acc))] |
| 189 | + |
| 190 | + (when (.hasRemaining buf) |
| 191 | + (.addLast buffer buf) |
| 192 | + (set! buffer-size (unchecked-int (p/+ buffer-size (.remaining buf))))) |
| 193 | + |
| 194 | + consumers))] |
| 195 | + |
| 196 | + (doit [c consumers] |
| 197 | + (trigger c)))) |
178 | 198 |
|
179 | 199 | (pushback [this ary offset length]
|
180 | 200 | (.pushback this
|
|
183 | 203 | (.limit (+ offset length)))))
|
184 | 204 |
|
185 | 205 | (take [_ ary offset length eager?]
|
| 206 | + |
186 | 207 | (let [out (-> (ByteBuffer/wrap ary)
|
187 | 208 | (.position offset)
|
188 | 209 | ^ByteBuffer (.limit (+ offset length))
|
189 |
| - .slice)] |
190 |
| - ((either |
191 |
| - [do] |
192 |
| - [u/with-lock lock]) |
193 |
| - |
194 |
| - (loop [] |
195 |
| - (when-let [^ByteBuffer in (.peek buffer)] |
196 |
| - (put in out) |
197 |
| - (when-not (.hasRemaining in) |
198 |
| - (.remove buffer)) |
199 |
| - (when (.hasRemaining out) |
200 |
| - (recur)))) |
201 |
| - |
202 |
| - (set! buffer-size (unchecked-int (p/- buffer-size (.position out)))) |
203 |
| - |
204 |
| - (when (and (p/<= buffer-size buffer-capacity) deferred) |
205 |
| - (d/success! deferred true) |
206 |
| - (set! deferred nil)) |
207 |
| - |
208 |
| - (if (or closed? |
209 |
| - (and (pos? (.position out)) |
210 |
| - (or eager? (not (.hasRemaining out))))) |
211 |
| - (d/success-deferred (.position out)) |
212 |
| - (let [d (d/deferred)] |
213 |
| - (.add consumers (Consumption. out d eager?)) |
214 |
| - d))))) |
| 210 | + .slice) |
| 211 | + |
| 212 | + [put take] |
| 213 | + |
| 214 | + ((either |
| 215 | + [do] |
| 216 | + [u/with-lock* lock]) |
| 217 | + |
| 218 | + (loop [] |
| 219 | + (when-let [^ByteBuffer in (.peek buffer)] |
| 220 | + (put in out) |
| 221 | + (when-not (.hasRemaining in) |
| 222 | + (.remove buffer)) |
| 223 | + (when (.hasRemaining out) |
| 224 | + (recur)))) |
| 225 | + |
| 226 | + (set! buffer-size |
| 227 | + (unchecked-int |
| 228 | + (p/- buffer-size |
| 229 | + (p/- |
| 230 | + (.position out) |
| 231 | + offset)))) |
| 232 | + |
| 233 | + [(when (and (p/<= buffer-size buffer-capacity) deferred) |
| 234 | + (let [d deferred] |
| 235 | + (set! deferred nil) |
| 236 | + d)) |
| 237 | + |
| 238 | + (if (or closed? |
| 239 | + (and (pos? (.position out)) |
| 240 | + (or eager? (not (.hasRemaining out))))) |
| 241 | + (d/success-deferred (.position out)) |
| 242 | + (let [d (d/deferred)] |
| 243 | + (.add consumers (Consumption. out d eager?)) |
| 244 | + d))])] |
| 245 | + |
| 246 | + (when put |
| 247 | + (d/success! put true)) |
| 248 | + |
| 249 | + take)) |
215 | 250 |
|
216 | 251 | (close [_]
|
217 |
| - ((either |
218 |
| - [do] |
219 |
| - [u/with-lock lock]) |
220 |
| - (set! closed? true) |
| 252 | + (when ((either |
| 253 | + [do] |
| 254 | + [u/with-lock* lock]) |
| 255 | + (when-not closed? |
| 256 | + (set! closed? true) |
| 257 | + true)) |
221 | 258 | (loop []
|
222 | 259 | (when-let [^Consumption c (.poll consumers)]
|
223 | 260 | (let [^ByteBuffer buf (.buf c)]
|
224 | 261 | (d/success! (.deferred c) (.position buf)))
|
225 |
| - (recur))) |
226 |
| - true)))) |
| 262 | + (recur)))) |
| 263 | + |
| 264 | + true))) |
227 | 265 |
|
228 | 266 | (defn pushback-stream [capacity]
|
229 | 267 | (SynchronizedPushbackByteStream.
|
|
0 commit comments