Skip to content

Commit

Permalink
Make sure we do not mutate the buffer and from-byte-buffer is idempot…
Browse files Browse the repository at this point in the history
…ent.

Mutable data structures, FUUUUUUUU
  • Loading branch information
michaelklishin committed Jul 13, 2012
1 parent f249d54 commit 04aa6b6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/clojure/clojurewerkz/support/string.clj
Expand Up @@ -125,5 +125,7 @@
"Creates a new string from the contents of the provided NIO byte buffer"
[^java.nio.ByteBuffer buf]
(let [ary (byte-array (.remaining buf))]
(.mark buf)
(.get buf ary)
(.reset buf)
(String. ^bytes ary)))
11 changes: 7 additions & 4 deletions test/clojurewerkz/support/string_test.clj
Expand Up @@ -101,7 +101,10 @@


(deftest test-from-byte-buffer
(let [s "a string"
buf (java.nio.ByteBuffer/wrap (.getBytes s "UTF-8"))
res (s/from-byte-buffer buf)]
(is (= s res))))
(let [s "a string"
buf (java.nio.ByteBuffer/wrap (.getBytes s "UTF-8"))
;; buffers are mutable, make sure the function is idempotent
res1 (s/from-byte-buffer buf)
res2 (s/from-byte-buffer buf)
res3 (s/from-byte-buffer buf)]
(is (= s res1 res2 res3))))

0 comments on commit 04aa6b6

Please sign in to comment.