Skip to content

Commit

Permalink
Extract read-ints as dedicated bytebuffer fn
Browse files Browse the repository at this point in the history
  • Loading branch information
athos committed Jan 4, 2024
1 parent 3fad1b5 commit bc6d442
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 10 deletions.
12 changes: 2 additions & 10 deletions src/cljam/io/cram/codecs/rans4x8.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@
(defn- decode0 [bb ^long n-out]
(let [freqs (read-frequencies0 bb)
cum-freqs (cumulative-frequencies freqs)
states (doto (int-array 4)
(aset 0 (int (bb/read-uint bb)))
(aset 1 (int (bb/read-uint bb)))
(aset 2 (int (bb/read-uint bb)))
(aset 3 (int (bb/read-uint bb))))
states (bb/read-ints bb 4)
out (byte-array n-out)]
(dotimes [i n-out]
(let [j (rem i 4)
Expand All @@ -87,11 +83,7 @@
freqs))
quarter (quot n-out 4)
truncated (* 4 quarter)
states (doto (int-array 4)
(aset 0 (int (bb/read-uint bb)))
(aset 1 (int (bb/read-uint bb)))
(aset 2 (int (bb/read-uint bb)))
(aset 3 (int (bb/read-uint bb))))
states (bb/read-ints bb 4)
last-syms (int-array 4)
out (byte-array n-out)]
(dotimes [i quarter]
Expand Down
9 changes: 9 additions & 0 deletions src/cljam/io/util/byte_buffer.clj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@
(.get bb buffer (int offset) (int length))
buffer))

(defn read-ints
"Reads 'length' ints to buffer starting from offset ints. Returns a new int-array if called without buffer."
(^ints [bb length]
(read-ints bb (int-array length) 0 length))
(^ints [^ByteBuffer bb ^ints buffer ^long offset ^long length]
(dotimes [i length]
(aset buffer (+ offset i) (.getInt bb)))
buffer))

(defn read-string
"Reads 'length' bytes. Returns a String."
[^ByteBuffer bb ^long length]
Expand Down
11 changes: 11 additions & 0 deletions test/cljam/io/util/byte_buffer_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
(is (= [0 0 0x34 0x12 0xF0 0]
(map #(bit-and % 0xFF) (bb/read-bytes bb (byte-array 6) 2 3)))))

(testing "read-ints"
(.reset bb)
(let [arr (bb/read-ints bb 2)]
(is (instance? (class (int-array 0)) arr))
(is (= [0xF0123456 0x789ABCDE] (map #(bit-and % 0xFFFFFFFF) arr))))

(.reset bb)
(bb/skip bb 1)
(is (= [0 0 0xDEF01234 0]
(map #(bit-and % 0xFFFFFFFF) (bb/read-ints bb (int-array 4) 2 1)))))

(testing "read-string"
(.reset bb)
(doseq [c "ABCDEFGH"]
Expand Down

0 comments on commit bc6d442

Please sign in to comment.