Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directly coerce to DataOutputStream without using output-stream #14

Merged
merged 2 commits into from
Jun 28, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 4 additions & 13 deletions src/clj_cbor/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,6 @@

;; ## Encoding Functions

(defn- data-output-stream
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it'd be better to keep this function and do something similar to 760124f - if the argument is already a DataOutputStream then return it, if it is an OutputStream already then wrap it directly, otherwise use io/output-stream to coerce it before wrapping.

"Coerce the argument to a `DataOutputStream`."
^DataOutputStream
[input]
(if (instance? DataOutputStream input)
input
(DataOutputStream. (io/output-stream input))))


(defn encode
"Encode a single value as CBOR data.

Expand All @@ -97,11 +88,11 @@
(encode default-codec value))
([encoder value]
(let [buffer (ByteArrayOutputStream.)]
(with-open [output (data-output-stream buffer)]
(with-open [output (DataOutputStream. buffer)]
(encode encoder output value))
(.toByteArray buffer)))
([encoder ^OutputStream output value]
(let [data-output (data-output-stream output)]
(let [data-output (DataOutputStream. output)]
(codec/write-value encoder data-output value))))


Expand All @@ -116,11 +107,11 @@
(encode-seq default-codec values))
([encoder values]
(let [buffer (ByteArrayOutputStream.)]
(with-open [output (data-output-stream buffer)]
(with-open [output (DataOutputStream. buffer)]
(encode-seq encoder output values))
(.toByteArray buffer)))
([encoder ^OutputStream output values]
(let [data-output (data-output-stream output)]
(let [data-output (DataOutputStream. output)]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would double-wrap an existing DataOutputStream, which is probably fine in practice but not ideal.

(transduce (map (partial encode encoder data-output)) + 0 values))))


Expand Down