-
Notifications
You must be signed in to change notification settings - Fork 5
/
impl.cljs
110 lines (90 loc) · 3.04 KB
/
impl.cljs
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
(ns deercreeklabs.lancaster.impl
(:require
[deercreeklabs.baracus :as ba]
[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]]))
(set! *warn-on-infer* true)
(defprotocol IResize
(embiggen [this min-added-bytes]))
(deftype OutputStream [^:mutable ba ^:mutable buflen ^:mutable pos]
u/IOutputStream
(write-byte [this b]
(let [new-pos (inc pos)]
(when (>= new-pos buflen)
(embiggen this 1))
(aset ba pos b)
(set! pos new-pos)))
(write-bytes [this source-ba num-bytes]
(let [new-pos (+ pos num-bytes)]
(when (>= new-pos buflen)
(embiggen this num-bytes))
(.set ba source-ba pos)
(set! pos new-pos)))
(write-bytes-w-len-prefix [this source-ba]
(let [num-bytes (count source-ba)]
(u/write-long-varint-zz this num-bytes)
(u/write-bytes this source-ba num-bytes)))
(write-utf8-string [this s]
(u/write-bytes-w-len-prefix this (ba/utf8->byte-array s)))
(write-float [this f]
(let [new-pos (+ pos 4)
dataview (js/DataView. (goog.object/get ba "buffer"))]
(when (>= new-pos buflen)
(embiggen this 4))
(.setFloat32 dataview pos f true)
(set! pos new-pos)))
(write-double [this d]
(let [new-pos (+ pos 8)
dataview (js/DataView. (goog.object/get ba "buffer"))]
(when (>= new-pos buflen)
(embiggen this 8))
(.setFloat64 dataview pos d true)
(set! pos new-pos)))
(to-byte-array [this]
(.slice ba 0 pos))
IResize
(embiggen [this min-added-bytes]
(let [num-new-bytes (max min-added-bytes buflen)
new-buf-len (+ buflen num-new-bytes)
new-buf (ba/byte-array new-buf-len)]
(.set new-buf ba)
(set! buflen new-buf-len)
(set! ba new-buf))))
(defn make-output-stream
[initial-size]
(->OutputStream (ba/byte-array initial-size) initial-size 0))
(defrecord InputStream [ba ^:mutable pos ^:mutable mark-pos]
u/IInputStream
(mark [this]
(set! mark-pos pos))
(read-byte [this]
(let [b (aget ba pos)]
(set! pos (inc pos))
b))
(read-bytes [this num-bytes]
(let [new-pos (+ pos num-bytes)
bs (ba/slice-byte-array ba pos new-pos)]
(set! pos new-pos)
bs))
(read-len-prefixed-bytes [this]
(let [num-bytes (u/read-long-varint-zz this)]
(u/read-bytes this num-bytes)))
(read-utf8-string [this]
(let [num-bytes (u/read-long-varint-zz this)
bytes (u/read-bytes this num-bytes)]
(ba/byte-array->utf8 bytes)))
(read-float [this]
(let [bs (u/read-bytes this 4)
dataview (js/DataView. (goog.object/get bs "buffer"))]
(.getFloat32 dataview 0 true)))
(read-double [this]
(let [bs (u/read-bytes this 8)
array-buf (goog.object/get bs "buffer")
dataview (js/DataView. array-buf)]
(.getFloat64 dataview 0 true)))
(reset-to-mark! [this]
(set! pos mark-pos)))
(defn make-input-stream [ba]
(->InputStream ba 0 0))