|
23 | 23 | ;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | 24 |
|
25 | 25 | (ns datoteka.io |
26 | | - "IO helpers (experimental, changes expected)." |
| 26 | + "IO helpers (a wrapper for clojure.java.io namespace)" |
| 27 | + (:refer-clojure :exclude [read flush]) |
27 | 28 | (:require |
28 | 29 | [clojure.core :as c] |
29 | 30 | [clojure.java.io :as jio] |
|
186 | 187 | [output] |
187 | 188 | (DataOutputStream. ^OutputStream output)) |
188 | 189 |
|
189 | | -(defn close! |
| 190 | +(defn close |
190 | 191 | "Close any AutoCloseable resource." |
191 | 192 | [^AutoCloseable stream] |
192 | 193 | (.close stream)) |
193 | 194 |
|
194 | | -(defn flush! |
| 195 | +(defn flush |
195 | 196 | "Flush the OutputStream" |
196 | 197 | [^OutputStream stream] |
197 | 198 | (.flush stream)) |
198 | 199 |
|
199 | | -(defn copy! |
| 200 | +(defn copy |
200 | 201 | "Efficiently copy data from `src` (should be instance of |
201 | 202 | InputStream) to the `dst` (which should be instance of |
202 | 203 | OutputStream). |
|
208 | 209 | (let [^bytes buff (byte-array buffer-size)] |
209 | 210 | (IOUtils/copyLarge ^InputStream src ^OutputStream dst (long offset) (long size) buff))) |
210 | 211 |
|
211 | | -(defn write! |
| 212 | +(defn write |
212 | 213 | "Writes content from `src` to the `dst`. |
213 | 214 |
|
214 | 215 | The `dst` argument should be an instance of OutputStream |
215 | 216 | If size is provided, no more than that bytes will be written to the |
216 | 217 | `dst`." |
217 | | - [src dst & {:keys [size offset close] :or {close false} :as opts}] |
| 218 | + [dst content & {:keys [size offset close] :or {close false} :as opts}] |
218 | 219 | (assert (output-stream? dst) "expected instance of OutputStream for dst") |
219 | 220 | (try |
220 | 221 | (cond |
221 | | - (instance? InputStream src) |
222 | | - (copy! src dst opts) |
| 222 | + (instance? InputStream content) |
| 223 | + (copy content dst opts) |
223 | 224 |
|
224 | 225 | ;; A faster write operation if we already have a byte array |
225 | 226 | ;; and we don't specify the size. |
226 | | - (and (bytes? src) |
| 227 | + (and (bytes? content) |
227 | 228 | (not size) |
228 | 229 | (not offset)) |
229 | 230 | (do |
230 | | - (IOUtils/writeChunked ^bytes src ^OutputStream dst) |
231 | | - (alength ^bytes src)) |
| 231 | + (IOUtils/writeChunked ^bytes content ^OutputStream dst) |
| 232 | + (alength ^bytes content)) |
232 | 233 |
|
233 | | - (string? src) |
| 234 | + (string? content) |
234 | 235 | (let [encoding (or (:encoding opts) "UTF-8") |
235 | | - data (.getBytes ^String src ^String encoding)] |
236 | | - (write! data dst opts)) |
| 236 | + data (.getBytes ^String content ^String encoding)] |
| 237 | + (write data dst opts)) |
237 | 238 |
|
238 | 239 | :else |
239 | | - (with-open [^InputStream input (jio/make-input-stream src opts)] |
240 | | - (copy! src dst opts))) |
| 240 | + (with-open [^InputStream input (jio/make-input-stream content opts)] |
| 241 | + (copy input dst opts))) |
241 | 242 |
|
242 | 243 | (finally |
243 | | - (flush! dst)))) |
| 244 | + (flush dst)))) |
244 | 245 |
|
245 | | -(defn write-to-file! |
| 246 | +(defn write-to-file |
246 | 247 | [src dst & {:keys [close] :or {close true} :as opts}] |
247 | 248 | (with-open [^OutputStream dst (jio/make-output-stream dst opts)] |
248 | | - (write! src dst opts))) |
| 249 | + (write src dst opts))) |
249 | 250 |
|
250 | | -(defn skip-fully |
| 251 | +(defn skip |
251 | 252 | [input offset] |
252 | 253 | (IOUtils/skipFully ^InputStream input (long offset))) |
253 | 254 |
|
254 | | -(defn read! |
| 255 | +(defn read |
255 | 256 | "Read all data or specified size input and return a byte array. |
256 | 257 | The `input` parameter should be instance of InputStream" |
257 | 258 | [input & {:keys [size]}] |
|
261 | 262 | input)] |
262 | 263 | (IOUtils/toByteArray ^InputStream input))) |
263 | 264 |
|
264 | | -(defn read-to-buffer! |
| 265 | +(defn read-to-buffer |
265 | 266 | "Read all data or specified size input and return a byte array. |
266 | 267 | The `input` parameter should be instance of InputStream" |
267 | 268 | [input buffer & {:keys [size offset]}] |
|
274 | 275 | (int offset) |
275 | 276 | (int size)))) |
276 | 277 |
|
277 | | - |
278 | | -(defn read-as-bytes |
279 | | - [input & {:as opts}] |
280 | | - (read! input opts)) |
281 | | - |
282 | 278 | (extend UnsynchronizedByteArrayOutputStream |
283 | 279 | jio/IOFactory |
284 | 280 | (assoc jio/default-streams-impl |
|
0 commit comments