From 04aa6b60f249d924bfba235f38f3ca4139212ada Mon Sep 17 00:00:00 2001 From: "Michael S. Klishin" Date: Fri, 13 Jul 2012 13:11:16 +0400 Subject: [PATCH] Make sure we do not mutate the buffer and from-byte-buffer is idempotent. Mutable data structures, FUUUUUUUU --- src/clojure/clojurewerkz/support/string.clj | 2 ++ test/clojurewerkz/support/string_test.clj | 11 +++++++---- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/clojure/clojurewerkz/support/string.clj b/src/clojure/clojurewerkz/support/string.clj index b44a2f3..766d788 100644 --- a/src/clojure/clojurewerkz/support/string.clj +++ b/src/clojure/clojurewerkz/support/string.clj @@ -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))) \ No newline at end of file diff --git a/test/clojurewerkz/support/string_test.clj b/test/clojurewerkz/support/string_test.clj index 3f664f9..c8f5270 100644 --- a/test/clojurewerkz/support/string_test.clj +++ b/test/clojurewerkz/support/string_test.clj @@ -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))))