Skip to content

Commit 9cac76e

Browse files
committed
fix ByteBuffer -> (seq-of ByteBuffer) position bug
1 parent f7a0293 commit 9cac76e

File tree

3 files changed

+75
-4
lines changed

3 files changed

+75
-4
lines changed

'

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
(ns byte-streams-test
2+
(:require
3+
[clojure.test :refer :all]
4+
[byte-streams :refer :all])
5+
(:import
6+
[java.io
7+
File]
8+
[java.nio
9+
ByteBuffer]))
10+
11+
(def text
12+
"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.")
13+
14+
(defn find-missing-roundtrips []
15+
(remove nil?
16+
(for [src (keys @@#'byte-streams/src->dst->conversion)
17+
dst (possible-conversions src)]
18+
(when-not (and (conversion-path src dst) (conversion-path dst src))
19+
[src dst]))))
20+
21+
(deftest test-roundtrips
22+
(let [pairwise-conversions (->> text
23+
possible-conversions
24+
(mapcat #(map list (repeat %) (possible-conversions %)))
25+
distinct)]
26+
(doseq [[src dst] pairwise-conversions]
27+
(is (= text (-> text (convert src) (convert dst) (convert String)))))))
28+
29+
(defn temp-file []
30+
(doto (File/createTempFile "byte-streams" ".tmp")
31+
(.deleteOnExit)))
32+
33+
(deftest test-transfer
34+
(doseq [dst (possible-conversions text)]
35+
(let [file (temp-file)]
36+
(transfer (convert text dst) file {:chunk-size 128})
37+
(is (= text (to-string file)))
38+
(is (= text (to-string (to-byte-buffers file {:chunk-size 128})))))))
39+
40+
(deftest test-byte-buffer-convert
41+
(let [arr (.getBytes ^String text)
42+
pos 13
43+
buf (doto (ByteBuffer/wrap arr) (.position pos))]
44+
(convert buf (class arr))
45+
(convert (repeat 2 buf) (class arr))
46+
(is (= pos (.position buf)))))
47+
48+
(deftest test-seq-of-byte-buffer
49+
(let [pos 10
50+
lim 20
51+
buf (doto ^ByteBuffer (convert text ByteBuffer)
52+
(.position pos)
53+
(.limit lim))
54+
s (convert buf String)]
55+
(are [x] (= s (convert x String))
56+
(convert buf (seq-of ByteBuffer) {:chunk-size 1})
57+
(convert buf (seq-of ByteBuffer) {:chunk-size 2})
58+
(convert buf (seq-of ByteBuffer) {:chunk-size 10})
59+
(convert buf (seq-of ByteBuffer) {:chunk-size 20}))))

src/byte_streams.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -443,13 +443,13 @@
443443
(def-conversion [ByteBuffer (seq-of ByteBuffer)]
444444
[buf {:keys [chunk-size]}]
445445
(if chunk-size
446-
(let [cnt (.remaining buf)
447-
indices (take-while #(< % cnt) (iterate #(+ % chunk-size) 0))]
446+
(let [lim (.limit buf)
447+
indices (range (.position buf) lim chunk-size)]
448448
(map
449449
#(-> buf
450450
.duplicate
451451
(.position %)
452-
^ByteBuffer (.limit (min cnt (+ % chunk-size)))
452+
^ByteBuffer (.limit (min lim (+ % chunk-size)))
453453
.slice)
454454
indices))
455455
[buf]))

test/byte_streams_test.clj

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,22 @@
3737
(is (= text (to-string file)))
3838
(is (= text (to-string (to-byte-buffers file {:chunk-size 128})))))))
3939

40-
(deftest test-bytebuffer-convert
40+
(deftest test-byte-buffer-convert
4141
(let [arr (.getBytes ^String text)
4242
pos 13
4343
buf (doto (ByteBuffer/wrap arr) (.position pos))]
4444
(convert buf (class arr))
4545
(convert (repeat 2 buf) (class arr))
4646
(is (= pos (.position buf)))))
47+
48+
(deftest test-seq-of-byte-buffer
49+
(let [pos 10
50+
lim 20
51+
buf (doto ^ByteBuffer (convert text ByteBuffer)
52+
(.position pos)
53+
(.limit lim))
54+
s (convert buf String)]
55+
(doseq [chunk-size (range 1 (+ pos lim))]
56+
(is (= s (convert
57+
(convert buf (seq-of ByteBuffer) {:chunk-size chunk-size})
58+
String))))))

0 commit comments

Comments
 (0)