Skip to content

Commit 76d3f54

Browse files
committed
conversions to CharSequences should be via ByteBuffers, not byte-arrays
1 parent f13c0c4 commit 76d3f54

File tree

4 files changed

+49
-54
lines changed

4 files changed

+49
-54
lines changed

project.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
(defproject byte-streams "0.2.0"
1+
(defproject byte-streams "0.2.1-SNAPSHOT"
22
:description "A simple way to handle the menagerie of Java byte represenations."
33
:license {:name "MIT License"
44
:url "http://opensource.org/licenses/MIT"}
55
:dependencies [[primitive-math "0.1.4"]
6-
[clj-tuple "0.2.1"]
6+
[clj-tuple "0.2.2"]
77
[manifold "0.1.0"]]
8-
:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0-alpha6"]
8+
:profiles {:dev {:dependencies [[org.clojure/clojure "1.7.0"]
99
[org.clojure/test.check "0.7.0"]
1010
[codox-md "0.2.0" :exclusions [org.clojure/clojure]]]}}
1111
:test-selectors {:stress :stress

src/byte_streams.clj

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -431,10 +431,9 @@
431431
;; channel => lazy-seq of byte-buffers
432432
(def-conversion [ReadableByteChannel (seq-of ByteBuffer)]
433433
[channel {:keys [chunk-size direct?] :or {chunk-size 4096, direct? false} :as options}]
434-
(when (.isOpen channel)
435-
(lazy-seq
436-
(when-let [b (proto/take-bytes! channel chunk-size options)]
437-
(cons b (convert channel (seq-of ByteBuffer) options))))))
434+
(lazy-seq
435+
(when-let [b (proto/take-bytes! channel chunk-size options)]
436+
(cons b (convert channel (seq-of ByteBuffer) options)))))
438437

439438
;; input-stream => channel
440439
(def-conversion ^{:cost 0} [InputStream ReadableByteChannel]
@@ -511,7 +510,7 @@
511510
[source options]
512511
(cs/decode-byte-source
513512
#(when-let [bytes (proto/take-bytes! source % options)]
514-
(convert bytes byte-array options))
513+
(convert bytes ByteBuffer options))
515514
#(when (proto/closeable? source)
516515
(proto/close source))
517516
options))
@@ -676,17 +675,16 @@
676675

677676
ReadableByteChannel
678677
(take-bytes! [this n {:keys [direct?] :or {direct? false}}]
679-
(when (.isOpen this)
680-
(let [^ByteBuffer buf (if direct?
681-
(ByteBuffer/allocateDirect n)
682-
(ByteBuffer/allocate n))]
683-
(while
684-
(and
685-
(.isOpen this)
686-
(pos? (.read this buf))))
687-
688-
(when (pos? (.position buf))
689-
(.flip buf)))))
678+
(let [^ByteBuffer buf (if direct?
679+
(ByteBuffer/allocateDirect n)
680+
(ByteBuffer/allocate n))]
681+
(while
682+
(and
683+
(.isOpen this)
684+
(pos? (.read this buf))))
685+
686+
(when (pos? (.position buf))
687+
(.flip buf))))
690688

691689
ByteBuffer
692690
(take-bytes! [this n _]

src/byte_streams/char_sequence.clj

Lines changed: 31 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -44,40 +44,39 @@
4444
^ByteBuffer extra-bytes
4545
close-fn
4646
byte-source]
47-
(let [chunk-size (long chunk-size)]
48-
(lazy-seq
49-
(let [num-bytes (+ (long
50-
(if extra-bytes
51-
(.remaining extra-bytes)
52-
0))
53-
(long chunk-size))
54-
len (long
55-
(Math/ceil
56-
(/ num-bytes
57-
(.averageCharsPerByte decoder))))
58-
out (CharBuffer/allocate len)]
47+
(lazy-seq
48+
(let [num-bytes (+ (long
49+
(if extra-bytes
50+
(.remaining extra-bytes)
51+
0))
52+
(long chunk-size))
53+
len (long
54+
(Math/ceil
55+
(/ num-bytes
56+
(.averageCharsPerByte decoder))))
57+
out (CharBuffer/allocate len)]
5958

60-
(if (and extra-bytes (= :overflow (decode decoder extra-bytes out)))
59+
(if (and extra-bytes (= :overflow (decode decoder extra-bytes out)))
6160

62-
;; we didn't even exhaust the overflow bytes, try again
63-
(cons
64-
out
65-
(lazy-char-buffer-sequence decoder chunk-size extra-bytes close-fn byte-source))
61+
;; we didn't even exhaust the overflow bytes, try again
62+
(cons
63+
out
64+
(lazy-char-buffer-sequence decoder chunk-size extra-bytes close-fn byte-source))
6665

67-
(if-let [in (byte-source chunk-size)]
68-
(let [result (decode decoder in out)]
69-
(cons
70-
(.flip out)
71-
(lazy-char-buffer-sequence
72-
decoder
73-
chunk-size
74-
(when (= :overflow result) in)
75-
close-fn
76-
byte-source)))
77-
(do
78-
(flush decoder out)
79-
(when close-fn (close-fn))
80-
(.flip out))))))))
66+
(if-let [in (byte-source chunk-size)]
67+
(let [result (decode decoder in out)]
68+
(cons
69+
(.flip out)
70+
(lazy-char-buffer-sequence
71+
decoder
72+
chunk-size
73+
(when (= :overflow result) in)
74+
close-fn
75+
byte-source)))
76+
(do
77+
(flush decoder out)
78+
(when close-fn (close-fn))
79+
(.flip out)))))))
8180

8281
(defn decode-byte-source
8382
[byte-source
@@ -90,9 +89,7 @@
9089
decoder (doto (.newDecoder (Charset/forName encoding))
9190
(.onMalformedInput action)
9291
(.onUnmappableCharacter action))
93-
s (lazy-char-buffer-sequence decoder chunk-size nil close-fn
94-
#(when-let [ary (byte-source %)]
95-
(ByteBuffer/wrap ary)))]
92+
s (lazy-char-buffer-sequence decoder chunk-size nil close-fn byte-source)]
9693
(reify
9794
java.io.Closeable
9895
(close [_] (when close-fn (close-fn)))

src/byte_streams/graph.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@
127127
(assoc-in [(Type. 'seq src) (Type. 'seq dst)]
128128
(Conversion. (fn [x options] (map #(f % options) x)) cost))
129129
(assoc-in [(Type. 'stream src) (Type. 'stream dst)]
130-
(Conversion. (fn [x options] (s/map #(f % options) x)) cost))))
130+
(Conversion. (fn [x options] (s/map #(f % options) x)) (+ cost 0.1)))))
131131
m')]
132132
(ConversionGraph. m')))
133133
(possible-sources [_]

0 commit comments

Comments
 (0)