-
Notifications
You must be signed in to change notification settings - Fork 5
/
impl.clj
86 lines (67 loc) · 2.44 KB
/
impl.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
(ns deercreeklabs.lancaster.impl
(:require
[cheshire.core :as json]
[deercreeklabs.lancaster.utils :as u]
[deercreeklabs.log-utils :as lu :refer [debugs]]
[schema.core :as s :include-macros true]
[taoensso.timbre :as timbre :refer [debugf errorf infof warnf]])
(:import
(com.google.common.io LittleEndianDataInputStream
LittleEndianDataOutputStream)
(java.io ByteArrayInputStream ByteArrayOutputStream)
(java.math BigInteger)))
(primitive-math/use-primitive-operators)
(defrecord OutputStream [baos ledos]
u/IOutputStream
(write-byte [this b]
(.writeByte ^LittleEndianDataOutputStream ledos b))
(write-bytes [this ba num-bytes]
(.write ^LittleEndianDataOutputStream ledos ba 0 num-bytes))
(write-bytes-w-len-prefix [this ba]
(let [num-bytes (count ba)]
(u/write-long-varint-zz this num-bytes)
(u/write-bytes this ba num-bytes)))
(write-utf8-string [this s]
(let [bytes (.getBytes ^String s "UTF-8")
num-bytes (count bytes)]
(u/write-long-varint-zz this num-bytes)
(u/write-bytes this bytes num-bytes)))
(write-float [this f]
(.writeFloat ^LittleEndianDataOutputStream ledos ^float f))
(write-double [this d]
(.writeDouble ^LittleEndianDataOutputStream ledos ^double d))
(to-byte-array [this]
(.toByteArray ^ByteArrayOutputStream baos)))
(defn make-output-stream
[initial-size]
(let [baos (ByteArrayOutputStream. initial-size)
ledos (LittleEndianDataOutputStream. baos)]
(->OutputStream baos ledos)))
(defn output-stream->byte-array [^ByteArrayOutputStream os]
(.toByteArray os))
(defrecord InputStream [^ByteArrayInputStream bais
^LittleEndianDataInputStream ledis]
u/IInputStream
(mark [this]
(.mark ledis (.available ledis)))
(read-byte [this]
(.readByte ledis))
(read-bytes [this num-bytes]
(let [ba (byte-array num-bytes)]
(.readFully ledis #^bytes ba 0 num-bytes)
ba))
(read-len-prefixed-bytes [this]
(let [num-bytes (u/read-long-varint-zz this)]
(u/read-bytes this num-bytes)))
(read-utf8-string [this]
(String. #^bytes (u/read-len-prefixed-bytes this) "UTF-8"))
(read-float [this]
(.readFloat ledis))
(read-double [this]
(.readDouble ledis))
(reset-to-mark! [this]
(.reset ledis)))
(defn make-input-stream [ba]
(let [bais (ByteArrayInputStream. ba)
ledis (LittleEndianDataInputStream. bais)]
(->InputStream bais ledis)))