Skip to content

Commit

Permalink
Stabilize datoteka.io ns api
Browse files Browse the repository at this point in the history
  • Loading branch information
niwinz committed Oct 21, 2024
1 parent 8dcb8fc commit ce1cd56
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 42 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog #

## Version 4.0 ##

- Stabilize datoteka.io API:
- Remove `!` from functions for make it similar to clojure.java.io api
- Reorder some parameters for make the api consistent
- Replace syncrhonized Input and Output streams with unsynchronizedd
version from apache commons-io

## Version 3.1 ##

- Add the ability to pass directory to `create-tempfile` and `tempfile`
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@ more detailed information.

```clojure
funcool/datoteka
{:git/tag "3.2.0"
{:git/tag "4.0.0"
:git/sha "6e4b4c2"
:git/url "https://github.com/funcool/datoteka.git"}

;; OR

nz.niwi/datoteka {:mvn/version "3.2.0-80"}
```

## Getting Started
Expand Down
50 changes: 23 additions & 27 deletions src/datoteka/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(ns datoteka.io
"IO helpers (experimental, changes expected)."
"IO helpers (a wrapper for clojure.java.io namespace)"
(:refer-clojure :exclude [read flush])
(:require
[clojure.core :as c]
[clojure.java.io :as jio]
Expand Down Expand Up @@ -186,17 +187,17 @@
[output]
(DataOutputStream. ^OutputStream output))

(defn close!
(defn close
"Close any AutoCloseable resource."
[^AutoCloseable stream]
(.close stream))

(defn flush!
(defn flush
"Flush the OutputStream"
[^OutputStream stream]
(.flush stream))

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

(defn write!
(defn write
"Writes content from `src` to the `dst`.
The `dst` argument should be an instance of OutputStream
If size is provided, no more than that bytes will be written to the
`dst`."
[src dst & {:keys [size offset close] :or {close false} :as opts}]
[dst content & {:keys [size offset close] :or {close false} :as opts}]
(assert (output-stream? dst) "expected instance of OutputStream for dst")
(try
(cond
(instance? InputStream src)
(copy! src dst opts)
(instance? InputStream content)
(copy content dst opts)

;; A faster write operation if we already have a byte array
;; and we don't specify the size.
(and (bytes? src)
(and (bytes? content)
(not size)
(not offset))
(do
(IOUtils/writeChunked ^bytes src ^OutputStream dst)
(alength ^bytes src))
(IOUtils/writeChunked ^bytes content ^OutputStream dst)
(alength ^bytes content))

(string? src)
(string? content)
(let [encoding (or (:encoding opts) "UTF-8")
data (.getBytes ^String src ^String encoding)]
(write! data dst opts))
data (.getBytes ^String content ^String encoding)]
(write data dst opts))

:else
(with-open [^InputStream input (jio/make-input-stream src opts)]
(copy! src dst opts)))
(with-open [^InputStream input (jio/make-input-stream content opts)]
(copy input dst opts)))

(finally
(flush! dst))))
(flush dst))))

(defn write-to-file!
(defn write-to-file
[src dst & {:keys [close] :or {close true} :as opts}]
(with-open [^OutputStream dst (jio/make-output-stream dst opts)]
(write! src dst opts)))
(write src dst opts)))

(defn skip-fully
(defn skip
[input offset]
(IOUtils/skipFully ^InputStream input (long offset)))

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

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


(defn read-as-bytes
[input & {:as opts}]
(read! input opts))

(extend UnsynchronizedByteArrayOutputStream
jio/IOFactory
(assoc jio/default-streams-impl
Expand Down
26 changes: 16 additions & 10 deletions test/datoteka/tests/test_core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
output (with-open [is (io/bytes-input-stream input)]
(with-open [os (io/bytes-output-stream 10)]
(io/copy! is os :size -1)
(io/read-as-bytes os)))]
(io/copy is os :size -1)
(with-open [is' (io/input-stream os)]
(io/read is'))))]

(t/is (= (seq input)
(seq output)))))
Expand All @@ -84,29 +85,34 @@
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
output (with-open [is (io/bytes-input-stream input)]
(with-open [os (io/bytes-output-stream 10)]
(io/copy! is os :offset 2)
(io/read-as-bytes os)))]
(io/copy is os :offset 2)
(with-open [is' (io/input-stream os)]
(io/read is'))))]

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

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

(t/is (= (vec input)
(vec output)))))

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

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

(t/deftest read-as-bytes-3
(t/deftest read-3
(let [input (byte-array [1 2 3 4 5 6 7 8 9 10])
output (io/read-as-bytes input :offset 4 :size 2)]
output (with-open [input (io/bytes-input-stream input)]
(io/skip input 4)
(io/read input :size 2))]
(t/is (= (vec output)
[5 6]))))

Expand Down

0 comments on commit ce1cd56

Please sign in to comment.