-
Notifications
You must be signed in to change notification settings - Fork 160
/
byte_utils.clj
56 lines (45 loc) · 1.53 KB
/
byte_utils.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
(ns crux.byte-utils
(:import [java.nio ByteBuffer ByteOrder]
java.security.MessageDigest
[java.util Arrays Comparator]
org.agrona.concurrent.UnsafeBuffer
crux.ByteUtils)
(:require [crux.memory :as mem]))
(set! *unchecked-math* :warn-on-boxed)
(defn long->bytes ^bytes [^long l]
(let [ub (UnsafeBuffer. (byte-array Long/BYTES))]
(.putLong ub 0 l ByteOrder/BIG_ENDIAN)
(.byteArray ub)))
(defn bytes->long ^long [^bytes data]
(let [ub (UnsafeBuffer. data)]
(.getLong ub 0 ByteOrder/BIG_ENDIAN)))
(defn sha1 ^bytes [^bytes bytes]
(.digest (MessageDigest/getInstance "SHA-1") bytes))
(defn bytes->hex ^String [^bytes bytes]
(ByteUtils/bytesToHex bytes))
(defn hex->bytes ^bytes [^String hex]
(ByteUtils/hexToBytes hex))
(defn byte-buffer->bytes ^bytes [^ByteBuffer b]
(if (.hasArray b)
(.array b)
(doto (byte-array (.remaining b))
(->> (.get b)))))
(defn compare-bytes
(^long [^bytes a ^bytes b]
(ByteUtils/compareBytes a b))
(^long [^bytes a ^bytes b max-length]
(ByteUtils/compareBytes a b max-length)))
(def ^java.util.Comparator bytes-comparator
ByteUtils/UNSIGNED_BYTES_COMPARATOR)
(defn bytes=?
([^bytes a ^bytes b]
(ByteUtils/equalBytes a b))
([^bytes a ^bytes b ^long max-length]
(ByteUtils/equalBytes a b max-length)))
(defn inc-unsigned-bytes!
([^bytes bs]
(inc-unsigned-bytes! bs (alength bs)))
([^bytes bs ^long prefix-length]
(some-> (UnsafeBuffer. bs 0 prefix-length)
(mem/inc-unsigned-buffer!)
(.byteArray))))