Skip to content

Commit 007f772

Browse files
committed
Merge pull request #3 from danburkert/master
Fix a bug in converting ByteBuffers
2 parents acc2c9c + 1415222 commit 007f772

File tree

2 files changed

+30
-12
lines changed

2 files changed

+30
-12
lines changed

src/byte_streams.clj

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@
415415
(doto buf
416416
.mark
417417
(.get ary 0 (.remaining buf))
418-
.rewind)
418+
.reset)
419419
ary))
420420
(let [^bytes ary (Array/newInstance Byte/TYPE (.remaining buf))]
421421
(doto buf .mark (.get ary) .reset)
@@ -432,8 +432,9 @@
432432
(ByteBuffer/allocateDirect len)
433433
(ByteBuffer/allocate len))]
434434
(doseq [^ByteBuffer b bufs]
435+
(.mark b)
435436
(.put buf b)
436-
(.rewind b))
437+
(.reset b))
437438
(when (satisfies? Closeable bufs)
438439
(close bufs))
439440
(.flip buf))))
@@ -442,13 +443,13 @@
442443
(def-conversion [ByteBuffer (seq-of ByteBuffer)]
443444
[buf {:keys [chunk-size]}]
444445
(if chunk-size
445-
(let [cnt (.remaining buf)
446-
indices (take-while #(< % cnt) (iterate #(+ % chunk-size) 0))]
446+
(let [lim (.limit buf)
447+
indices (range (.position buf) lim chunk-size)]
447448
(map
448449
#(-> buf
449450
.duplicate
450451
(.position %)
451-
^ByteBuffer (.limit (min cnt (+ % chunk-size)))
452+
^ByteBuffer (.limit (min lim (+ % chunk-size)))
452453
.slice)
453454
indices))
454455
[buf]))
@@ -718,7 +719,7 @@
718719
([x options]
719720
(convert x (seq-of ByteBuffer) options)))
720721

721-
(defn ^bytes to-byte-array
722+
(defn ^"[B" to-byte-array
722723
"Converts the object to a byte-array."
723724
([x]
724725
(to-byte-array x nil))
@@ -774,6 +775,3 @@
774775
(to-byte-sink x nil))
775776
([x options]
776777
(convert x ByteSink options)))
777-
778-
779-

test/byte_streams_test.clj

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44
[byte-streams :refer :all])
55
(:import
66
[java.io
7-
File]))
7+
File]
8+
[java.nio
9+
ByteBuffer]
10+
[java.util
11+
Arrays]))
812

913
(def text
1014
"The suburb of Saffron Park lay on the sunset side of London, as red and ragged as a cloud of sunset. It was built of a bright brick throughout; its sky-line was fantastic, and even its ground plan was wild. It had been the outburst of a speculative builder, faintly tinged with art, who called its architecture sometimes Elizabethan and sometimes Queen Anne, apparently under the impression that the two sovereigns were identical. It was described with some justice as an artistic colony, though it never in any definable way produced any art. But although its pretensions to be an intellectual centre were a little vague, its pretensions to be a pleasant place were quite indisputable. The stranger who looked for the first time at the quaint red houses could only think how very oddly shaped the people must be who could fit in to them. Nor when he met the people was he disappointed in this respect. The place was not only pleasant, but perfect, if once he could regard it not as a deception but rather as a dream. Even if the people were not \"artists,\" the whole was nevertheless artistic. That young man with the long, auburn hair and the impudent face—that young man was not really a poet; but surely he was a poem. That old gentleman with the wild, white beard and the wild, white hat—that venerable humbug was not really a philosopher; but at least he was the cause of philosophy in others. That scientific gentleman with the bald, egg-like head and the bare, bird-like neck had no real right to the airs of science that he assumed. He had not discovered anything new in biology; but what biological creature could he have discovered more singular than himself? Thus, and thus only, the whole place had properly to be regarded; it had to be considered not so much as a workshop for artists, but as a frail but finished work of art. A man who stepped into its social atmosphere felt as if he had stepped into a written comedy.")
@@ -35,5 +39,21 @@
3539
(is (= text (to-string file)))
3640
(is (= text (to-string (to-byte-buffers file {:chunk-size 128})))))))
3741

38-
39-
42+
(deftest test-byte-buffer-convert
43+
(let [arr (.getBytes ^String text)
44+
pos 13
45+
buf (doto (ByteBuffer/wrap arr) (.position pos))]
46+
(to-byte-array buf)
47+
(to-byte-array (repeat 2 buf))
48+
(is (= pos (.position buf)))))
49+
50+
(deftest test-seq-of-byte-buffer
51+
(let [buf (doto ^ByteBuffer (to-byte-buffer "quick brown fox")
52+
(.position 3)
53+
(.limit 6))
54+
arr (to-byte-array buf)]
55+
(doseq [chunk-size (range 1 (+ 1 (.capacity buf)))]
56+
(is (Arrays/equals
57+
(to-byte-array (convert buf (seq-of ByteBuffer) {:chunk-size chunk-size}))
58+
arr)))
59+
(is (empty? (convert buf (seq-of ByteBuffer) {:chunk-size 0})))))

0 commit comments

Comments
 (0)