Skip to content

Commit

Permalink
Add appropiate marks for platform dependent code (jvm mainly at this …
Browse files Browse the repository at this point in the history
…moment).
  • Loading branch information
Andrey Antukh committed Feb 26, 2015
1 parent 0d0466d commit f19e112
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/bytebuf/buffer.cljx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns bytebuf.buffer
"Buffer abstractions."
#+clj
(:import java.nio.ByteBuffer
io.netty.buffer.ByteBuf
io.netty.buffer.ByteBufAllocator))
Expand Down Expand Up @@ -36,6 +37,7 @@
;; NIO & Netty Buffer implementations
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#+clj
(extend-type ByteBuffer
IBufferShort
(read-short [buff pos]
Expand Down Expand Up @@ -87,6 +89,7 @@
(.put buff data 0 size)
(.position buff oldpos))))

#+clj
(extend-type ByteBuf
IBufferShort
(read-short [buff pos]
Expand Down Expand Up @@ -136,25 +139,30 @@
;; Public Api
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

#+clj
(def ^{:private true}
allocator (ByteBufAllocator/DEFAULT))

(defmulti allocate
(fn [size & [{:keys [type impl] :or {type :heap impl :nio}}]]
(fn [size & [{:keys [type impl] :or {type :heap impl #+clj :nio #+cljs :es6}}]]
[type impl]))

#+clj
(defmethod allocate [:heap :nio]
[size & _]
(ByteBuffer/allocate size))

#+clj
(defmethod allocate [:direct :nio]
[size & _]
(ByteBuffer/allocateDirect size))

#+clj
(defmethod allocate [:heap :netty]
[size & _]
(.heapBuffer allocator size))

#+clj
(defmethod allocate [:direct :netty]
[size & _]
(.directBuffer allocator size))
7 changes: 6 additions & 1 deletion src/bytebuf/bytes.cljx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(ns bytebuf.bytes
#+clj
(:import java.util.Arrays
java.security.SecureRandom))

#+clj
(defn random-bytes
"Generate a byte array of scpecified length with random
bytes taken from secure random number generator.
Expand All @@ -14,17 +16,20 @@
(.nextBytes sr buffer)
buffer)))

#+clj
(defn slice
"Given a byte array, get a copy of it. If offset
and limit is provided, a slice will be returned."
[^bytes input ^long offset ^long limit]
(Arrays/copyOfRange input offset limit))

#+clj
(defn equals?
"Check if two byte arrays are equals."
[^bytes a ^bytes b]
(Arrays/equals a b))

#+clj
(defn zeropad!
"Add zero byte padding to the given byte array
to the remaining bytes after specified data length."
Expand All @@ -34,7 +39,7 @@
(defn zeropad-count
"Given a byte array, returns a number of bytes
allocated with zero padding (zero byte)."
[^bytes input]
[input]
(let [mark (byte 0)]
(reduce (fn [sum index]
(let [value (aget input index)]
Expand Down
16 changes: 16 additions & 0 deletions src/bytebuf/spec.cljx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,18 @@
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(deftype AssociativeSpec [data dict types]
#+clj
clojure.lang.Counted
#+clj
(count [_]
(count types))

#+cljs
ICounted
#+cljs
(-count [_]
(count types))

IStaticSize
(size [_]
(reduce #(+ %1 (proto/size %2)) 0 types))
Expand All @@ -45,10 +53,18 @@
(- written pos))))

(deftype IndexedSpec [types]
#+clj
clojure.lang.Counted
#+clj
(count [_]
(count types))

#+cljs
ICounted
#+cljs
(-count [_]
(count types))

IStaticSize
(size [_]
(reduce #(+ %1 (proto/size %2)) 0 types))
Expand Down

0 comments on commit f19e112

Please sign in to comment.