Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ pom.xml.asc
.\#*
/out
/*-init.clj
/doc/dist/
/doc/dist/
.claude/settings.local.json
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
:profiles
{:dev {:dependencies [[org.clojure/tools.namespace "0.2.11"]]
:aliases {"test-all" ["with-profile" "dev,1.8:dev,1.7:dev" "test"]}
:global-vars {*warn-on-reflection* false}
:global-vars {*warn-on-reflection* true}
:plugins [[funcool/codeina "0.5.0"]
[lein-ancient "0.6.15"]]}
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
Expand Down
32 changes: 16 additions & 16 deletions src/octet/buffer.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@

#?(:clj
(defn- set-current-bytebuffer-byte-order!
[buff]
[^ByteBuffer buff]
(case *byte-order*
:big-endian (.order buff ByteOrder/BIG_ENDIAN)
:little-endian (.order buff ByteOrder/LITTLE_ENDIAN))))
Expand Down Expand Up @@ -137,7 +137,7 @@
(read-ulong [buff pos]
(set-current-bytebuffer-byte-order! buff)
(let [val (.getLong buff pos)
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) .array)]
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) (.array))]
(bigint (BigInteger. 1 magnitude))))
(write-ulong [buff pos value]
(set-current-bytebuffer-byte-order! buff)
Expand All @@ -163,30 +163,30 @@
IBufferByte
(read-byte [buff pos]
(set-current-bytebuffer-byte-order! buff)
(.get buff pos))
(.get buff ^int pos))
(write-byte [buff pos value]
(set-current-bytebuffer-byte-order! buff)
(.put buff pos value))
(.put buff ^int pos (byte value)))
(read-ubyte [buff pos]
(set-current-bytebuffer-byte-order! buff)
(let [val (.get buff pos)]
(let [val (.get buff ^int pos)]
(bit-and 0xFF (short val))))
(write-ubyte [buff pos value]
(set-current-bytebuffer-byte-order! buff)
(let [value (.byteValue (Short. (short value)))]
(.put buff pos value)))
(.put buff ^int pos value)))

IBufferBytes
(read-bytes [buff pos size]
(let [tmpbuf (byte-array size)
oldpos (.position buff)]
(.position buff pos)
(.position buff ^int pos)
(.get buff tmpbuf)
(.position buff oldpos)
tmpbuf))
(write-bytes [buff pos size data]
(let [oldpos (.position buff)]
(.position buff pos)
(.position buff ^int pos)
(.put buff data 0 size)
(.position buff oldpos)))

Expand Down Expand Up @@ -239,7 +239,7 @@
(.setLong buff pos value)))
(read-ulong [buff pos]
(let [val (read-long buff pos)
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) .array)]
^bytes magnitude (-> (ByteBuffer/allocate 8) (.putLong val) (.array))]
(bigint (BigInteger. 1 magnitude))))
(write-ulong [buff pos value]
(let [value (.longValue (bigint value))]
Expand Down Expand Up @@ -280,10 +280,10 @@
IBufferBytes
(read-bytes [buff pos size]
(let [tmpbuf (byte-array size)]
(.getBytes buff pos tmpbuf)
(.getBytes buff ^int pos tmpbuf)
tmpbuf))
(write-bytes [buff pos size data]
(.setBytes buff pos data 0 size))
(.setBytes buff ^int pos ^bytes data 0 ^int size))

IBufferLimit
(get-capacity [buff]
Expand Down Expand Up @@ -437,7 +437,7 @@
(let [offset (.-byteOffset buff)
buffer (.-buffer buff)]
(js/Int8Array. buffer (+ offset pos) size)))
(write-bytes [buff pos size data]
(write-bytes [buff pos _size data]
(doseq [i (range (.-length data))]
(.setInt8 buff (+ pos i) (aget data i))))

Expand Down Expand Up @@ -471,8 +471,8 @@
This function is defined as multimethod and you can
extend it with your own bytebuffer implementations
if you want or need it."
(fn [size & [{:keys [type impl] :or {type :heap
impl #?(:clj :nio :cljs :es6)}}]]
(fn [_size & [{:keys [type impl] :or {type :heap
impl #?(:clj :nio :cljs :es6)}}]]
[type impl]))

#?(:clj
Expand All @@ -488,12 +488,12 @@
#?(:clj
(defmethod allocate [:heap :netty]
[size & _]
(.heapBuffer allocator size)))
(.heapBuffer ^ByteBufAllocator allocator size)))

#?(:clj
(defmethod allocate [:direct :netty]
[size & _]
(.directBuffer allocator size)))
(.directBuffer ^ByteBufAllocator allocator size)))

#?(:cljs
(defmethod allocate [:heap :es6]
Expand Down
14 changes: 7 additions & 7 deletions src/octet/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@

(ns octet.core
(:refer-clojure :exclude [read byte float double short long bytes into repeat])
(:require [octet.spec :as spec]
#?(:cljs
[octet.util :as util :include-macros true]
:clj
[octet.util :as util])
(:require [octet.buffer :as buffer]
[octet.spec :as spec]
[octet.spec.basic :as basic-spec]
[octet.spec.string :as string-spec]
[octet.spec.collections :as coll-spec]
[octet.spec.reference :as ref-spec]
[octet.buffer :as buffer]))
[octet.spec.string :as string-spec]
#?(:cljs
[octet.util :as util :include-macros true]
:clj
[octet.util :as util])))

(util/defalias compose spec/compose)
(util/defalias spec spec/spec)
Expand Down
11 changes: 6 additions & 5 deletions src/octet/spec.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@

For more examples see the `spec` function docstring."
(:refer-clojure :exclude [type read float double long short byte bytes repeat])
(:require [octet.buffer :as buffer]
[octet.util :refer [assoc-ordered]]))
(:require [octet.util :refer [assoc-ordered]]))

;; --- Protocols

Expand Down Expand Up @@ -72,10 +71,12 @@
ISpecDynamicSize
(size* [_ data]
(reduce (fn [acc [field data]]
(let [type (field dict)]
(if-let [type (field dict)]
(if (satisfies? ISpecDynamicSize type)
(+ acc (size* type data))
(+ acc (size type)))))
(+ acc (size type)))
; ignore vals that are not in spec
acc))
0
(into [] data)))

Expand Down Expand Up @@ -240,7 +241,7 @@

ISpecDynamicSize
(size* [_ data]
(reduce (fn [acc [index data]]
(reduce (fn [acc [_index data]]
(if (satisfies? ISpecSize type)
(+ acc (size type))
(+ acc (size* type data))))
Expand Down
4 changes: 2 additions & 2 deletions src/octet/spec/string.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
(defn zeropad-count
"Given a byte array, returns a number of bytes
allocated with zero padding (zero byte)."
[input]
[^bytes input]
(let [mark (byte 0)]
(reduce (fn [sum index]
(let [value (aget input index)]
Expand Down Expand Up @@ -89,7 +89,7 @@
(js/Int8Array. buff)))

(defn arraycopy
[^bytes input ^bytes output ^long length]
[^bytes input ^bytes output ^long _length]
(reduce (fn [_ i]
(aset output i (aget input i)))
nil
Expand Down
55 changes: 31 additions & 24 deletions src/octet/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
;; OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

(ns octet.util
(:require [clojure.string :as str :refer [join]]
[octet.buffer :as bfr])
#?(:clj (:require [clojure.string :as str]
[octet.buffer :as buffer]))

#?(:clj (:import java.util.Arrays)))

Expand All @@ -34,9 +34,10 @@
(def ~sym ~sym2)
(alter-meta! (var ~sym) merge (dissoc (meta (var ~sym2)) :name))))

(defn assoc-ordered [a-map key val & rest]
(defn assoc-ordered
"assoc into an array-map, keeping insertion order. The normal clojure
assoc function switches to hash maps on maps > size 10 and loses insertion order"
[a-map key val & rest]
(let [kvs (interleave (concat (keys a-map) (list key))
(concat (vals a-map) (list val)))
ret (apply array-map kvs)]
Expand All @@ -55,7 +56,7 @@
fh (fn [_ b]
(let [h (Integer/toHexString (bit-and b 0xFF))]
(if (<= 0 b 15) (str "0" h) h)))]
(join (reductions fh (fh 0 f) r)))))
(str/join (reductions fh (fh 0 f) r)))))

#?(:clj
(defn byte->ascii
Expand All @@ -64,57 +65,63 @@
(if (<= 32 (bit-and byte 0xFF) 127) (char byte) \.)))

#?(:clj
(defn- bytes->ascii [^bytes bytes]
(defn- bytes->ascii
"returns a 16-per-line printable ascii view of the bytes"
[^bytes bytes]
(->> bytes
(map byte->ascii)
(partition 16 16 " ")
(map join))))
(map str/join))))

#?(:clj
(defn- format-hex-line [^String hex-line]
(defn- format-hex-line
"formats a 'line' (32 hex chars) of hex output"
[^String hex-line]
(->> hex-line
(partition-all 4)
(map join)
(map str/join)
(split-at 4)
(map #(join " " %))
(join " "))))
(map #(str/join " " %))
(str/join " "))))

#?(:clj
(defn- bytes->hexdump [^bytes bytes]
(defn- bytes->hexdump
"formats a byte array to a sequence of formatted hex lines"
[^bytes bytes]
(->> bytes
bytes->hex
(partition 32 32 (join (repeat 32 " ")))
(bytes->hex)
(partition 32 32 (str/join (repeat 32 " ")))
(map format-hex-line))))

#?(:clj
(defn- copy-bytes [bytes offset size]
(defn- copy-bytes
"utility function - copy bytes, return new byte array"
^bytes
[^bytes bytes ^long offset ^long size]
(let [size (if (nil? size) (alength bytes) size)]
(if (and (= 0 offset) (= (alength bytes) size))
bytes ; short circuit
(java.util.Arrays/copyOfRange bytes
offset
(+ offset size))))))
(Arrays/copyOfRange bytes
offset
^long (+ offset size))))))

#?(:clj
(defn get-dump-bytes
"utility function - return byte array from offset offset and with
size size for nio ByteBuffer, netty ByteBuf, byte array, and String"
^bytes
[x offset size]
(cond
(and (satisfies? octet.buffer/IBufferBytes x)
(satisfies? octet.buffer/IBufferLimit x))
(let [size (if (nil? size) (octet.buffer/get-capacity x) size)]
(octet.buffer/read-bytes x offset size))
(and (satisfies? buffer/IBufferBytes x)
(satisfies? buffer/IBufferLimit x))
(let [size (if (nil? size) (buffer/get-capacity x) size)]
(buffer/read-bytes x offset size))

(instance? (type (byte-array 0)) x)
(copy-bytes x offset size)

(instance? String x)
(copy-bytes (.getBytes x) offset size))))
(copy-bytes (.getBytes ^String x) offset size))))


; Example usage of hex-dump
Expand Down Expand Up @@ -156,9 +163,9 @@
ascii (bytes->ascii bytes)
offs (map #(format "%08x" %)
(range offset (+ offset size 16) 16))
header (str " " (join (repeat 68 "-")))
header (str " " (str/join (repeat 68 "-")))
border (if frame "|" "")
lines (map #(str border %1 ": " %2 " " %3 border) offs dump ascii)
lines (if frame (concat [header] lines [header]) lines)
result (join \newline lines)]
result (str/join \newline lines)]
(if print (println result) result))))
10 changes: 5 additions & 5 deletions test/octet/tests/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@
#?(:clj
(t/deftest allocate-heap-nio-buffer
(let [buffer (buf/allocate 16)]
(t/is (not (.isDirect buffer)))
(t/is (not (.isDirect ^ByteBuffer buffer)))
(t/is (instance? ByteBuffer buffer)))))

#?(:clj
(t/deftest allocate-direct-nio-buffer
(let [buffer (buf/allocate 16 {:type :direct})]
(t/is (.isDirect buffer))
(t/is (.isDirect ^ByteBuffer buffer))
(t/is (instance? ByteBuffer buffer)))))

#?(:clj
(t/deftest allocate-heap-netty-buffer
(let [buffer (buf/allocate 16 {:type :heap :impl :netty})]
(t/is (not (.isDirect buffer)))
(t/is (not (.isDirect ^ByteBuf buffer)))
(t/is (instance? ByteBuf buffer)))))

#?(:clj
(t/deftest allocate-direct-netty-buffer
(let [buffer (buf/allocate 16 {:type :direct :impl :netty})]
(t/is (.isDirect buffer))
(t/is (.isDirect ^ByteBuf buffer))
(t/is (instance? ByteBuf buffer)))))

#?(:clj
Expand All @@ -99,7 +99,7 @@
buffer (buf/allocate 12)
data [500]]
(t/is (= (buf/write! buffer data spec {:offset 3}) 4))
(t/is (= (.getInt buffer 3) 500)))))
(t/is (= (.getInt ^ByteBuffer buffer 3) 500)))))

#?(:cljs
(t/deftest allocate-direct-es6-buffer
Expand Down