Skip to content

Commit ce1cd56

Browse files
committed
Stabilize datoteka.io ns api
1 parent 8dcb8fc commit ce1cd56

File tree

4 files changed

+48
-42
lines changed

4 files changed

+48
-42
lines changed

CHANGES.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog #
22

3+
## Version 4.0 ##
4+
5+
- Stabilize datoteka.io API:
6+
- Remove `!` from functions for make it similar to clojure.java.io api
7+
- Reorder some parameters for make the api consistent
8+
- Replace syncrhonized Input and Output streams with unsynchronizedd
9+
version from apache commons-io
10+
311
## Version 3.1 ##
412

513
- Add the ability to pass directory to `create-tempfile` and `tempfile`

README.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,9 @@ more detailed information.
1717

1818
```clojure
1919
funcool/datoteka
20-
{:git/tag "3.2.0"
20+
{:git/tag "4.0.0"
2121
:git/sha "6e4b4c2"
2222
:git/url "https://github.com/funcool/datoteka.git"}
23-
24-
;; OR
25-
26-
nz.niwi/datoteka {:mvn/version "3.2.0-80"}
2723
```
2824

2925
## Getting Started

src/datoteka/io.clj

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2424

2525
(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])
2728
(:require
2829
[clojure.core :as c]
2930
[clojure.java.io :as jio]
@@ -186,17 +187,17 @@
186187
[output]
187188
(DataOutputStream. ^OutputStream output))
188189

189-
(defn close!
190+
(defn close
190191
"Close any AutoCloseable resource."
191192
[^AutoCloseable stream]
192193
(.close stream))
193194

194-
(defn flush!
195+
(defn flush
195196
"Flush the OutputStream"
196197
[^OutputStream stream]
197198
(.flush stream))
198199

199-
(defn copy!
200+
(defn copy
200201
"Efficiently copy data from `src` (should be instance of
201202
InputStream) to the `dst` (which should be instance of
202203
OutputStream).
@@ -208,50 +209,50 @@
208209
(let [^bytes buff (byte-array buffer-size)]
209210
(IOUtils/copyLarge ^InputStream src ^OutputStream dst (long offset) (long size) buff)))
210211

211-
(defn write!
212+
(defn write
212213
"Writes content from `src` to the `dst`.
213214
214215
The `dst` argument should be an instance of OutputStream
215216
If size is provided, no more than that bytes will be written to the
216217
`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}]
218219
(assert (output-stream? dst) "expected instance of OutputStream for dst")
219220
(try
220221
(cond
221-
(instance? InputStream src)
222-
(copy! src dst opts)
222+
(instance? InputStream content)
223+
(copy content dst opts)
223224

224225
;; A faster write operation if we already have a byte array
225226
;; and we don't specify the size.
226-
(and (bytes? src)
227+
(and (bytes? content)
227228
(not size)
228229
(not offset))
229230
(do
230-
(IOUtils/writeChunked ^bytes src ^OutputStream dst)
231-
(alength ^bytes src))
231+
(IOUtils/writeChunked ^bytes content ^OutputStream dst)
232+
(alength ^bytes content))
232233

233-
(string? src)
234+
(string? content)
234235
(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))
237238

238239
: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)))
241242

242243
(finally
243-
(flush! dst))))
244+
(flush dst))))
244245

245-
(defn write-to-file!
246+
(defn write-to-file
246247
[src dst & {:keys [close] :or {close true} :as opts}]
247248
(with-open [^OutputStream dst (jio/make-output-stream dst opts)]
248-
(write! src dst opts)))
249+
(write src dst opts)))
249250

250-
(defn skip-fully
251+
(defn skip
251252
[input offset]
252253
(IOUtils/skipFully ^InputStream input (long offset)))
253254

254-
(defn read!
255+
(defn read
255256
"Read all data or specified size input and return a byte array.
256257
The `input` parameter should be instance of InputStream"
257258
[input & {:keys [size]}]
@@ -261,7 +262,7 @@
261262
input)]
262263
(IOUtils/toByteArray ^InputStream input)))
263264

264-
(defn read-to-buffer!
265+
(defn read-to-buffer
265266
"Read all data or specified size input and return a byte array.
266267
The `input` parameter should be instance of InputStream"
267268
[input buffer & {:keys [size offset]}]
@@ -274,11 +275,6 @@
274275
(int offset)
275276
(int size))))
276277

277-
278-
(defn read-as-bytes
279-
[input & {:as opts}]
280-
(read! input opts))
281-
282278
(extend UnsynchronizedByteArrayOutputStream
283279
jio/IOFactory
284280
(assoc jio/default-streams-impl

test/datoteka/tests/test_core.clj

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@
7474
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
7575
output (with-open [is (io/bytes-input-stream input)]
7676
(with-open [os (io/bytes-output-stream 10)]
77-
(io/copy! is os :size -1)
78-
(io/read-as-bytes os)))]
77+
(io/copy is os :size -1)
78+
(with-open [is' (io/input-stream os)]
79+
(io/read is'))))]
7980

8081
(t/is (= (seq input)
8182
(seq output)))))
@@ -84,29 +85,34 @@
8485
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
8586
output (with-open [is (io/bytes-input-stream input)]
8687
(with-open [os (io/bytes-output-stream 10)]
87-
(io/copy! is os :offset 2)
88-
(io/read-as-bytes os)))]
88+
(io/copy is os :offset 2)
89+
(with-open [is' (io/input-stream os)]
90+
(io/read is'))))]
8991

9092
(t/is (= (vec output)
9193
[3 4 5 6 7 8 9 10]))))
9294

93-
(t/deftest read-as-bytes-1
95+
(t/deftest read-1
9496
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
95-
output (io/read-as-bytes input)]
97+
output (with-open [input (io/bytes-input-stream input)]
98+
(io/read input))]
9699

97100
(t/is (= (vec input)
98101
(vec output)))))
99102

100-
(t/deftest read-as-bytes-2
103+
(t/deftest read-2
101104
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
102-
output (io/read-as-bytes input :size 4)]
105+
output (with-open [input (io/bytes-input-stream input)]
106+
(io/read input :size 4))]
103107

104108
(t/is (= (vec output)
105109
[1 2 3 4]))))
106110

107-
(t/deftest read-as-bytes-3
111+
(t/deftest read-3
108112
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
109-
output (io/read-as-bytes input :offset 4 :size 2)]
113+
output (with-open [input (io/bytes-input-stream input)]
114+
(io/skip input 4)
115+
(io/read input :size 2))]
110116
(t/is (= (vec output)
111117
[5 6]))))
112118

0 commit comments

Comments
 (0)